代码改变世界

Linq to xml 读取xml文件或xml字符串

2019-03-02 15:24  防民之口甚于防川  阅读(584)  评论(0编辑  收藏  举报

 

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

                               };