【发布时间】:2012-06-15 06:56:26
【问题描述】:
我有一段 xml 作为 Web 服务的响应。
<Production>
<creator>...</creator>
<creator.date_of_birth/>
<creator.date_of_death/>
<creator.history/>
<creator.lref>426</creator.lref>
<creator.qualifier/>
<creator.role/>
<creator.role.lref/>
<production.notes/>
<production.place>France</production.place>
<production.place.lref>30</production.place.lref>
</Production>
<production.period>19th century</production.period>
<production.period.lref>13</production.period.lref>
<Production_date>
<production.date.end>1863</production.date.end>
<production.date.end.prec>circa.</production.date.end.prec>
<production.date.start>1863</production.date.start>
<production.date.start.prec>dated</production.date.start.prec>
</Production_date>
下面是我获取值的 LINQ 代码:
allrecs = (from XElement c in recordSet.Descendants("recordList")
where c.HasElements
from x in c.Elements("record")
select new CollectionRecord()
{
production_place = x.Element("Production").Element("production.place").Value,
production_period = x.Element("production.period").Value,
production_start_date = x.Element("Production_date").Element("production.date.start").Value,
production_end_date = x.Element("Production_date").Element("production.date.end").Value,
}).ToList <CollectionRecord>();
我发现的问题是 - 我无法获得“production.date.start”和“production.date.end”的值,但上述代码适用于“production.period”。 xml 结构和这些元素中的数据没有任何问题。根据我的理解,它不适用于任何类似 - “a.b.c”的元素,但它适用于 - “a.b”,因为我已经检查过其他类似元素并且它不起作用!
【问题讨论】:
-
请再次检查您的 XML。 b'cas 我在粘贴 XMLFile 时遇到错误,它显示错误“XML 文档不能有多个根级别元素”也在这里验证您的 XML validome.org/xml/validate
-
@Shankar - 它只是粘贴在这里的文档的一个片段 - 只需将它包装在一个节点中就可以了
-
@SidP 好的...下面发布的答案已解决您的查询...或仍在搜索?
标签: c# asp.net linq linq-to-xml