Linq to XML——我的查询出了什么问题
我有这个 xml 文档(不,我没有编写这个模式)。
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<wmversion>3</wmversion>
<summary day="362" >
<item key="SomeAttribute">
<item key="1">0.33</item>
<item key="10">3.32</item>
<item key="11">0.23</item>
<item key="12">1.06</item>
<item key="13">0.09</item>
<item key="2">0.35</item>
<item key="3">0.72</item>
<item key="4">0.61</item>
<item key="5">1.01</item>
<item key="6">0.10</item>
<item key="7">0.50</item>
<item key="8">1.27</item>
<item key="9">3.01</item>
</item>
...
现在我尝试查询此信息,例如:
XDocument doc = XDocument.Load(@"C:\Test.xml");
var q = from d in doc.Descendants("summary")
where d.Element("item").Attribute("key").Value == "SomeAttribute"
select new { LengendKey = d.Attribute("key").Value, ElapsedTime = d.Element("item").Value };
我返回 0 个项目而不是列表。有谁知道我在这里做错了什么?
谢谢,比尔·N
I have this xml document (and no I didn't make up this schema).
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<wmversion>3</wmversion>
<summary day="362" >
<item key="SomeAttribute">
<item key="1">0.33</item>
<item key="10">3.32</item>
<item key="11">0.23</item>
<item key="12">1.06</item>
<item key="13">0.09</item>
<item key="2">0.35</item>
<item key="3">0.72</item>
<item key="4">0.61</item>
<item key="5">1.01</item>
<item key="6">0.10</item>
<item key="7">0.50</item>
<item key="8">1.27</item>
<item key="9">3.01</item>
</item>
...
Now I'm trying to query this information like:
XDocument doc = XDocument.Load(@"C:\Test.xml");
var q = from d in doc.Descendants("summary")
where d.Element("item").Attribute("key").Value == "SomeAttribute"
select new { LengendKey = d.Attribute("key").Value, ElapsedTime = d.Element("item").Value };
I'm returning 0 items instead of the list. Does anyone know what i'm doing wrong here?
Thanks, Bill N
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你还不清楚你想做什么。
在此处的代码中,
d
是“summary”的所有后代,它们本身具有具有正确属性的“item”元素。在您发布的 XML 中,只有 1 个“summary”后代,并且它没有任何具有正确属性的“item”子项。
我也对行
LengendKey = d.Attribute("key").Value, ElapsedTime = d.Element("item").Value
- isd
感到困惑这里假设叶节点(具有键 1、2、3 等) - 适合语句的第一部分,或者父节点,其下面有 'item' 元素 - 适合语句的第二部分?两者不能同时存在。你可能想要
I guess it's not really clear what you are trying to do.
In your code here,
d
is all the descendents of 'summary' which themselves have an 'item' element with the correct attribute.In the XML you've posted, there's only 1 descendent of 'summary', and it doesn't have any 'item' children with the correct attribute.
I'm also confused about the line
LengendKey = d.Attribute("key").Value, ElapsedTime = d.Element("item").Value
- isd
here supposed to the leaf node (which has key 1, 2, 3 etc) - which would fit the first part of the statement, or the parent node which has the 'item' elements underneath it - which fits the second part of the statement? It can't be both at the same time.You probably want
这对你有用吗?或者您在寻找其他东西吗?
Does this work for you ? Or Are you looking for something else ?
这有效:
This works: