XML序列化,添加命名空間,添加聲明頭,添加節點前綴

xml序列化方法app

/// <summary>
/// 將一個對象序列化爲XML字符串
/// </summary>
/// <param name="o">要序列化的對象</param>
/// <param name="encoding">編碼方式</param>
/// <returns>序列化產生的XML字符串</returns>
public static string XmlSerialize(object o, Encoding encoding)
{
    if (o == null)
        throw new ArgumentNullException("o");
    if (encoding == null)
        throw new ArgumentNullException("encoding");

    string xml = "";
    try
    {
        using (MemoryStream stream = new MemoryStream())
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = encoding;

            //OmitXmlDeclaration表示不生成聲明頭,默認是false,OmitXmlDeclaration爲true,會去掉<?xml version="1.0" encoding="UTF-8"?>
            //settings.OmitXmlDeclaration = true;

            XmlWriter writer = XmlWriter.Create(stream, settings);

            //強制指定命名空間,覆蓋默認的命名空間,能夠添加多個,若是要在xml節點上添加指定的前綴,能夠在跟節點的類上面添加[XmlRoot(Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = false)],Namespace指定哪一個值,xml節點添加的前綴就是哪一個命名空間(這裏會添加ceb)
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("ceb", "http://www.chinaport.gov.cn/ceb");
            namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XmlSerializer serializer = new XmlSerializer(o.GetType());
            serializer.Serialize(writer, o, namespaces);
            writer.Close();

            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream, encoding))
            {
                xml = reader.ReadToEnd();
            }
        }
    }
    catch (Exception ex)
    {
        
    }
    return xml;
}

對象屬性:測試

[XmlRoot(Namespace = "http://www.chinaport.gov.cn/ceb")]
public class CEB311Message
{
    [XmlAttribute("guid")]
    public string Guid { get; set; }

    [XmlAttribute("version")]
    public string Version { get; set; }

    public Order Order { get; set; }

    public BaseTransfer BaseTransfer { get; set; }
}

public class Order
{
    public OrderHead OrderHead { get; set; }

    [XmlElement("OrderList")]
    public List<OrderList> OrderList { get; set; }
}

public class BaseTransfer
{
    public string copCode { get; set; }
    
    public string copName { get; set; }
    
    public string dxpMode { get; set; }
    
    public string dxpId { get; set; }
    
    public string note { get; set; }
}

public class OrderHead
{
    //......
}

public class OrderList
{
    //......
}

序列化之後的xml報文:ui

<?xml version="1.0" encoding="utf-8"?>
<ceb:CEB311Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" guid="F9BF2981-B2E2-4621-A15D-74E25AE6F8FD" version="1.0" xmlns:ceb="http://www.chinaport.gov.cn/ceb">
    <ceb:Order>
        <ceb:OrderHead>
            <ceb:guid>AF55F05A-1176-474A-A560-631EFAC83061</ceb:guid>
            <ceb:appType>1</ceb:appType>
            <ceb:appTime>20160708143049</ceb:appTime>
            <ceb:appStatus>2</ceb:appStatus>
            <ceb:orderType>I</ceb:orderType>
            <ceb:orderNo>XSC2016070200002</ceb:orderNo>
            <ceb:ebpCode>Alibaba888888</ceb:ebpCode>
            <ceb:ebpName>杭州阿里巴巴淘寶電子商務有限公司</ceb:ebpName>
            <ceb:ebcCode>IE123456789</ceb:ebcCode>
            <ceb:ebcName>廈門xxx軟件科技有限公司</ceb:ebcName>
            <ceb:goodsValue>902.50</ceb:goodsValue>
            <ceb:freight>0</ceb:freight>
            <ceb:discount>0</ceb:discount>
            <ceb:taxTotal>90.25</ceb:taxTotal>
            <ceb:acturalPaid>902.50</ceb:acturalPaid>
            <ceb:currency>142</ceb:currency>
            <ceb:buyerRegNo>青帝</ceb:buyerRegNo>
            <ceb:buyerName>葉青</ceb:buyerName>
            <ceb:buyerIdType>1</ceb:buyerIdType>
            <ceb:buyerIdNumber>350583198909067413</ceb:buyerIdNumber>
            <ceb:payCode />
            <ceb:payName />
            <ceb:payTransactionId>ZFB20160628170422</ceb:payTransactionId>
            <ceb:batchNumbers />
            <ceb:consignee>葉青</ceb:consignee>
            <ceb:consigneeTelephone>18201010101</ceb:consigneeTelephone>
            <ceb:consigneeAddress>福建省xxx</ceb:consigneeAddress>
            <ceb:consigneeDistrict>350203</ceb:consigneeDistrict>
            <ceb:note />
        </ceb:OrderHead>
        <ceb:OrderList>
            <ceb:gnum>1</ceb:gnum>
            <ceb:itemNo>TestZS20160712242</ceb:itemNo>
            <ceb:itemName>測試海關總署商品</ceb:itemName>
            <ceb:itemDescribe />
            <ceb:barCode />
            <ceb:unit>122</ceb:unit>
            <ceb:qty>5</ceb:qty>
            <ceb:price>180.50</ceb:price>
            <ceb:totalPrice>902.50</ceb:totalPrice>
            <ceb:currency>142</ceb:currency>
            <ceb:country>304</ceb:country>
            <ceb:note />
        </ceb:OrderList>
    </ceb:Order>
    <ceb:BaseTransfer>
        <ceb:copCode>440316277F</ceb:copCode>
        <ceb:copName>xxx</ceb:copName>
        <ceb:dxpMode>DXP</ceb:dxpMode>
        <ceb:dxpId>DXPENTTEST510001</ceb:dxpId>
        <ceb:note />
    </ceb:BaseTransfer>
</ceb:CEB311Message>
CEB311Message類頭上的[XmlRoot(Namespace = "http://www.chinaport.gov.cn/ceb")],Namespaces指定哪個值,就能夠添加對應命名空間的前綴
相關文章
相關標籤/搜索