天天看點

Linq to xml 讀取xml檔案或xml字元串

XMLFile1.xml:

XDocument Contacts = XDocument.Load("XMLFile1.xml");

//XElement Contacts = XElement.Parse(xmldata);

var Elist = from t in Contacts.Descendants("HEADER")

select new

{

CREATION_DATE = t.Element("CREATION_DATE").Value.Replace("\n", ""),

ITEM_NUMBER = t.Element("ITEM_NUMBER").Value.Replace("\n", ""),

ITEM_DESC = t.Element("ITEM_DESC").Value.Replace("\n", ""),

ITEM_CATEGORY_CODE = t.Element("ITEM_CATEGORY_CODE").Value.Replace("\n", ""),

PRIMARY_UOM_CODE = t.Element("PRIMARY_UOM_CODE").Value.Replace("\n", "") //注意此處用到 attribute

};

  XDocument Contacts =XDocument.Load("XMLFile1.xml");

                    //XElement Contacts = XElement.Parse(xmldata);

                    var Elist =from t in Contacts.Descendants("HEADER")

                               selectnew

                               {

                                   CREATION_DATE = t.Element("CREATION_DATE").Value.Replace("\n", ""),

                                   ITEM_NUMBER = t.Element("ITEM_NUMBER").Value.Replace("\n", ""),

                                   ITEM_DESC = t.Element("ITEM_DESC").Value.Replace("\n", ""),

                                   ITEM_CATEGORY_CODE = t.Element("ITEM_CATEGORY_CODE").Value.Replace("\n", ""),

                                   PRIMARY_UOM_CODE = t.Element("PRIMARY_UOM_CODE").Value.Replace("\n", "")   //注意此處用到 attribute

                               };

轉載于:https://www.cnblogs.com/ggdxx/p/4942640.html

c#