Linq to XML——我的查询出了什么问题

发布于 2024-10-15 08:29:36 字数 1166 浏览 4 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女皇必胜 2024-10-22 08:29:36

我想你还不清楚你想做什么。

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 }

在此处的代码中,d 是“summary”的所有后代,它们本身具有具有正确属性的“item”元素。

在您发布的 XML 中,只有 1 个“summary”后代,并且它没有任何具有正确属性的“item”子项。

我也对行 LengendKey = d.Attribute("key").Value, ElapsedTime = d.Element("item").Value - is d 感到困惑这里假设叶节点(具有键 1、2、3 等) - 适合语句的第一部分,或者父节点,其下面有 'item' 元素 - 适合语句的第二部分?两者不能同时存在。

你可能想要

// first get the summary; it is a descendant of doc & there's only one.
var summary = doc.Descendants("summary").Single();

// get the element with 'SomeAttribute' from the summary;
// if there's only even going to be one then you can call Single here
var item = summary.Elements().Single(e => e.Name == "item" 
    && e.Attribute("key").Value == "SomeAttribute");

var q = item.Elements().Select(e => new 
    { LengendKey = e.Attribute("key").Value, ElapsedTime = e.Value });

I guess it's not really clear what you are trying to do.

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 }

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 - is d 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

// first get the summary; it is a descendant of doc & there's only one.
var summary = doc.Descendants("summary").Single();

// get the element with 'SomeAttribute' from the summary;
// if there's only even going to be one then you can call Single here
var item = summary.Elements().Single(e => e.Name == "item" 
    && e.Attribute("key").Value == "SomeAttribute");

var q = item.Elements().Select(e => new 
    { LengendKey = e.Attribute("key").Value, ElapsedTime = e.Value });
没有伤那来痛 2024-10-22 08:29:36
var q = doc.Descendants("summary")
           .Where(x => x.Element("item").Attribute("key").Value == "SomeAttribute")
           .SelectMany(x => x.Descendants())
           .Select( x => new  
               { LengendKey = x.Attribute("key").Value, ElapsedTime = x.Value  });

这对你有用吗?或者您在寻找其他东西吗?

var q = doc.Descendants("summary")
           .Where(x => x.Element("item").Attribute("key").Value == "SomeAttribute")
           .SelectMany(x => x.Descendants())
           .Select( x => new  
               { LengendKey = x.Attribute("key").Value, ElapsedTime = x.Value  });

Does this work for you ? Or Are you looking for something else ?

一桥轻雨一伞开 2024-10-22 08:29:36

这有效:

var q = doc.Descendants("summary").Descendants("item").Descendants("item")
    .Select ( d => new { LengendKey = d.Attribute("key").Value, ElapsedTime = d.Value });

This works:

var q = doc.Descendants("summary").Descendants("item").Descendants("item")
    .Select ( d => new { LengendKey = d.Attribute("key").Value, ElapsedTime = d.Value });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文