通过linq读取root中的所有节点值

时间:2016-08-24 12:16:13

标签: c# xml linq

我有以下XML文档结构,它由一些xml格式的web rest API返回。

 
-<spls>
 -<metadata>    
   <total_elements>4056</total_elements>
   <elements_per_page>100</elements_per_page>
   <total_pages>41</total_pages>
   <current_page>1</current_page>
   <current_url>https://dailymed.nlm.nih.gov/dailymed/services/v2/spls.xml?published_date=2016-07-10&published_date_comparison=gte</current_url>
   <previous_page>null</previous_page>
   <previous_page_url>null</previous_page_url>
   <next_page>2</next_page>
   <next_page_url>https://dailymed.nlm.nih.gov/dailymed/services/v2/spls.xml?published_date=2016-07-10&published_date_comparison=gte&page=2&pagesize=100</next_page_url>
   <db_published_date>Aug 23, 2016 05:14:15PM EST</db_published_date>
 </metadata>
-<spl>
  <setid>029acfa1-81f0-490c-ad46-ec6f19591293</setid>
  <spl_version>3</spl_version>
  <title>QUETIAPINE FUMARATE TABLET [REMEDYREPACK INC.]</title>
  <published_date>Aug 23, 2016</published_date>
 </spl>

-<spl>
  <setid>02cdae31-5b23-452b-9046-7819ef51f3ed</setid>
  <spl_version>1</spl_version>
  <title>LISINOPRIL TABLET [CARDINAL HEALTH]</title>
  <published_date>Aug 23, 2016</published_date>
 </spl>

现在我想将这个XML解析为我的自定义对象,如下所示

class spl
{
   public string setid { get; set; }
   public string spl_version { get; set; }
   public string title { get; set; }
   public string published_date { get; set; } 
}

所以我的linq应该返回一个 SPL 列表,其中包含其属性的所有值。

我试过这样的事情:

xmlDoc = XDocument.Parse(sr.ReadToEnd());

IEnumerable<spl> result = from c in xmlDoc.Descendants("spls")
                          select new spl()
                          {
                              setid = (string)c.Attribute("setid"),
                              spl_version = (string)c.Attribute("spl_version"),
                              title = (string)c.Attribute("title"),
                              published_date = (string)c.Attribute("published_date")
                          };

2 个答案:

答案 0 :(得分:2)

你的linq中有2个错误:

  1. Descendants方法中,您可以指定要查找的元素的名称。所以spl代替spls
  2. 对象所需的值是元素而非属性,因此请使用c.Element(...)代替c.Attribute(...)
  3. 所以:

    var result = from c in xmlDoc.Descendants("spl")
                 select new spl()
                 {
                     setid = c.Element("setid").Value,
                     spl_version = c.Element("spl_version").Value,
                     title = c.Element("title").Value,
                     published_date = c.Element("published_date").Value
                 };
    

答案 1 :(得分:0)

试试这个

参考链接 http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/reading-xml-file-through-linq-a-few-tips/

xmlDoc = XDocument.Parse(sr.ReadToEnd());

IEnumerable<spl> result = from c in xmlDoc.Descendants("spl")
                          select new spl()
                          {
                              setid = (string)c.Elements("setid"),
                              spl_version = (string)c.Elements("spl_version"),
                              title = (string)c.Elements("title"),
                              published_date = (string)c.Elements("published_date")
                          };