C# XDocument的使用

XDocument保存XML文件


XDocument document = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));

                XElement header = new XElement("SFIS-ISN",
                    new XElement("MSGTYPE", mAsusReportHeader.MsgType),
                    new XElement("MSGDATE", mAsusReportHeader.MsgDate),
                    new XElement("MSGID", mAsusReportHeader.MsgId),
                    new XElement("SENDID", mAsusReportHeader.SendId),
                    new XElement("RECEID", mAsusReportHeader.ReceId)

                    );

int nStep = 0;
//此处数据为集合,可自行添加数据源
 foreach (AsusReportMasterEntity mAsusReportMaster in listAsusReportMaster)
                {
                    nStep++;
                    XElement master = new XElement("MASTER");

                    master.Add(new XElement("DOCID", mAsusReportMaster.DocId),
                        new XElement("DOCDATETIME", mAsusReportMaster.DocDatetime),
                        new XElement("VENDOR_TYPE", mAsusReportMaster.VendorType),
                        new XElement("VENDOR_ID", mAsusReportMaster.VendorId),
                        new XElement("BU", mAsusReportMaster.Bu),
                        new XElement("HUB_TYPE", mAsusReportMaster.HubType),
                        new XElement("ISN", mAsusReportMaster.Isn),
                        new XElement("ORI_ISN", mAsusReportMaster.OriIsn),
                        new XElement("SSN", mAsusReportMaster.Ssn),
                        new XElement("ASUS_PN", mAsusReportMaster.AsusPn),
                        new XElement("SO_NO", mAsusReportMaster.SoNo),
                        new XElement("SO_LINE", mAsusReportMaster.SoLine),
                        new XElement("DN_NO", mAsusReportMaster.DnNo),
                        new XElement("DN_LINE", mAsusReportMaster.DnLine),
                        new XElement("PO_NO", mAsusReportMaster.PoNo),
                        new XElement("PO_LINE", mAsusReportMaster.PoLine),
                        new XElement("ORDER_QTY", mAsusReportMaster.OrderQty),
                        new XElement("SHIP_QTY", mAsusReportMaster.ShipQty),
                        new XElement("SHIP_DATE", mAsusReportMaster.ShipDate),
                        new XElement("PALLET_ID", mAsusReportMaster.PalletId),
                        new XElement("PALLET_NO", mAsusReportMaster.PalletNo),
                        new XElement("PALLET_NW", mAsusReportMaster.PalletNw == 0 ? (object)null : mAsusReportMaster.PalletNw),
                        new XElement("PALLET_GW", mAsusReportMaster.PalletGw == 0 ? (object)null : mAsusReportMaster.PalletGw),
                        new XElement("CARTON_ID", mAsusReportMaster.CartonId),
                        new XElement("CARTON_NW", mAsusReportMaster.CartonNw),
                        new XElement("CARTON_GW", mAsusReportMaster.CartonGw),
                        new XElement("BOX_QTY", mAsusReportMaster.BoxQty),
                        new XElement("WARRANTY", mAsusReportMaster.Warranty),
                        new XElement("WARR_EXP_DATE", mAsusReportMaster.WarrExpDate),
                        new XElement("DOA", mAsusReportMaster.Doa)
                        );
 header.Add(master);
}

<pre name="code" class="csharp">document.Add(header);
//保存文档
document.Save(sFileName);

 

生成后的格式如下:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<SFIS-ISN>
  <MSGTYPE>SFIS-ISN</MSGTYPE>
  <MSGDATE>20160823212607</MSGDATE>
  <MSGID></MSGID>
  <SENDID>S09A0093</SENDID>
  <RECEID>ASUS</RECEID>
  <MASTER>
    <DOCID></DOCID>
    <DOCDATETIME>20160823212607</DOCDATETIME>
    <VENDOR_TYPE>EMS</VENDOR_TYPE>
    <VENDOR_ID>S09A0093</VENDOR_ID>
    <BU>ZenFone</BU>
    <HUB_TYPE></HUB_TYPE>
    <ISN>E281C672638004</ISN>
    <ORI_ISN>E281C672638004</ORI_ISN>
    <SSN>G8AXGY02H886K7L</SSN>
    <ASUS_PN>90AX0085-M00810</ASUS_PN>
    <SO_NO></SO_NO>
    <SO_LINE></SO_LINE>
    <DN_NO>15160400869</DN_NO>
    <DN_LINE>12957997</DN_LINE>
    <PO_NO></PO_NO>
    <PO_LINE></PO_LINE>
    <ORDER_QTY>180</ORDER_QTY>
    <SHIP_QTY>180</SHIP_QTY>
    <SHIP_DATE>20160823</SHIP_DATE>
    <PALLET_ID>DG800000189</PALLET_ID>
    <PALLET_NO>189</PALLET_NO>
    <PALLET_NW>73.05</PALLET_NW>
    <PALLET_GW>92</PALLET_GW>
    <CARTON_ID>CE281F1608110026</CARTON_ID>
    <CARTON_NW>3.77</CARTON_NW>
    <CARTON_GW>4.06</CARTON_GW>
    <BOX_QTY>10</BOX_QTY>
    <WARRANTY></WARRANTY>
    <WARR_EXP_DATE></WARR_EXP_DATE>
    <DOA>N</DOA>   
  </MASTER>
</SFIS-ISN>


  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值