C# 通过linq读取根目录中具有there属性的所有节点值
我有以下XML文档的结构,它是由一些WebRESTAPI以XML格式返回的C# 通过linq读取根目录中具有there属性的所有节点值,c#,xml,linq,C#,Xml,Linq,我有以下XML文档的结构,它是由一些WebRESTAPI以XML格式返回的 -<spls> -<metadata> <total_elements>4056</total_elements> <elements_per_page>100</elements_per_page> <total_pages>41</total_pages> <current_page
-<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>
因此,我的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")
};
xmlDoc=XDocument.Parse(sr.ReadToEnd());
IEnumerable result=来自xmlDoc.subjects(“SPL”)中的c
选择新的spl()
{
setid=(字符串)c.Attribute(“setid”),
spl_版本=(字符串)c.Attribute(“spl_版本”),
title=(字符串)c.Attribute(“title”),
发布日期=(字符串)c.Attribute(“发布日期”)
};
您的linq中有两个错误:
子体
方法中,指定要查找的元素的名称。因此spl
而不是spl
c.Element(…)
而不是c.Attribute(…)
xmlDoc=XDocument.Parse(sr.ReadToEnd());
IEnumerable result=来自xmlDoc.subjects(“spl”)中的c
选择新的spl()
{
setid=(字符串)c.Elements(“setid”),
spl_版本=(字符串)c.Elements(“spl_版本”),
title=(字符串)c.Elements(“title”),
发布日期=(字符串)c.Elements(“发布日期”)
};
@rahularyansharma-很高兴它有帮助:)您可以将xml反序列化为C类型的对象。
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")
};
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
};
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")
};