简体   繁体   English

使用linq读取xml数据的简单方法

[英]simple way to read xml data using linq

I have a xml structure like this. 我有这样的xml结构。 Can anyone help with a simple linq function to read this xml structure.The itemEntry node repeats according to data. 任何人都可以使用简单的linq函数来帮助读取此xml结构。itemEntry节点根据数据重复。 I tried to read the xml using the method below,but i am getting no records in the list. 我试图使用下面的方法读取xml,但是列表中没有记录。 Is this method here correct way to get the details... 这是获取详细信息的正确方法吗...

List<CX_ITEMLIST> sList =
    (from e in XDocument.Load(param.FileName).Root.Elements("itemEntry")
     select new CX_ITEMLIST
     {
         TITLE = (string)e.Element("title"),
         YEAR = (string)e.Element("year"),
         ITEMNAME = (string)e.Element("itemname"),
         CATRYLIST =
         (
             from p in e.Elements("categorylist").Elements("categories")
             select new CATLIST
             {
                 IDTYPE = (string)p.Element("categoryid"),
                 IDNUMBER = (string)p.Element("categoryName")
             }).ToList()
     }).ToList();
<itemslist>
 <itemInformation>
  <itemdate>01/23/2014</itemdate>
  <itemcount>57</itemcount>
 </itemInformation>
 <itemEntry>
  <title>Title1</title>
  <year>2013</title>
  <itemname>testname</itemname>
  <categorylist>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  </categorylist>
 </itemEntry>
 <itemEntry>
  <title>Title1</title>
  <year>2013</title>
  <itemname>testname</itemname>
  <categorylist>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  </categorylist>
 </itemEntry>
</itemslist>

您应该尝试使用XDocument

XDocument xdoc = XDocument.Load("file.xml");

The System.Xml.XLinq namespace contains some awesome objects to make this easy. System.Xml.XLinq命名空间包含一些很棒的对象,可以使此操作变得容易。

var xDoc = XDocument.Parse(xml); // load your xml string, or use XDocument.Load() to load an xml file

var itemEntries = xDoc
    .Root                       // refers to itemEntries node
    .Descendants("itemEntry");  // gets all itemEntry nodes in an IEnumerable object 

This gets you an IEnumerable<XNode> of all the itemEntry nodes. 这将为您提供所有itemEntry节点的IEnumerable<XNode>

From there you can do what you need, save the values to a business object, etc. 在这里,您可以执行所需的操作,将值保存到业务对象等。

The above method works properly, i found the issue, my xml tag was having namespace attribute. 上面的方法正常工作,我发现了问题,我的xml标记具有名称空间属性。 i tried to get the namespace and append it with Elements while reading 我试图获取名称空间并在阅读时将其附加元素

XNamespace ns = xDocument.Root.Attribute("xmlns").Value;

List<CX_ITEMLIST> sList =
    (from e in XDocument.Load(param.FileName).Root.Elements(ns + "itemEntry")
     select new CX_ITEMLIST
     {
         TITLE = (string)e.Element(ns + "title"),
         YEAR = (string)e.Element(ns + "year"),
         ITEMNAME = (string)e.Element(ns + "itemname"),
         CATRYLIST =
         (
             from p in e.Elements(ns + "categorylist").Elements(ns + "categories")
             select new CATLIST
             {
                 IDTYPE = (string)p.Element(ns + "categoryid"),
                 IDNUMBER = (string)p.Element(ns + "categoryName")
             }).ToList()
     }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM