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);
}

document.Add(header);
//保存文档
document.Save(sFileName);

 
  

生成后的格式如下:



  SFIS-ISN
  20160823212607
  
  S09A0093
  ASUS
  
    
    20160823212607
    EMS
    S09A0093
    ZenFone
    
    E281C672638004
    E281C672638004
    G8AXGY02H886K7L
    90AX0085-M00810
    
    
    15160400869
    12957997
    
    
    180
    180
    20160823
    DG800000189
    189
    73.05
    92
    CE281F1608110026
    3.77
    4.06
    10
    
    
    N   
  


你可能感兴趣的:(C#)