C# XDocument的使用

来源:互联网 发布:快速排序算法的例子 编辑:程序博客网 时间:2024/06/07 20:48

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>


0 0
原创粉丝点击