Linq to XML不能正常工作

问题描述:

我开始理解这个整个Linq到XML的东西,但是,我发现自己感到困惑,因为为什么某个查询不会像预期的那样工作。Linq to XML不能正常工作

错误很明显在查询中,但我找不到它。也许你们中的一个人可以在黑暗中散发光芒。

这是我的XML:

<folder title="test1" id="f6e27403" index="1"> 
<list index="1" name="content1" title="" id="5702f74a"></list> 
<list index="2" name="content2" title="" id="a39db279"></list> 
<list index="3" name="content3" title="" id="6f48a9e7"></list> 
<list index="4" name="content4" title="" id="85d2823c"></list> 
<list index="5" name="content5" title="" id="669d28f4"> 
    <theme index="1" name="Q1" type="r1theme" title="" id="6bf9dbce" created="2015-06-17 10:02:35" modified="2015-06-18 17:34:30"> 
    <properties> 
     <field index="1" name="remark"/> 
     <field index="2" name="title">SERIES A</field> 
    </properties> 
    <list> 
     <theme index="1" name="q" type="lb" title="" id="b021319d-03ae-1c7a-ab31-fb081bae4570" created="2015-05-28 11:17:09" modified="2015-06-19 11:23:13"> 
      <properties> 
       <field index="1" name="remark"/> 
       <field index="2" name="content">Test</field> 
      </properties> 
      <list> 
       <item index="1" name="q" type="item" title="" id="52ddcb77" created="2015-05-28 11:19:35" modified="2015-05-28 11:19:35"> 
        <field index="1" name="query">content 1</field> 
        <field index="2" name="remark"/> 
       </item> 
       <item index="2" name="q" type="item" title="" id="2f5793db" created="2015-05-28 11:18:00" modified="2015-05-28 11:18:00"> 
        <field index="1" name="query">content 2</field> 
        <field index="2" name="remark"/> 
       </item> 
      </list> 
     </theme> 
    </list> 
    </theme> 

    <theme index="2" name="Q2" type="r1theme" title="" id="6bf9dbce" created="2015-06-17 10:02:35" modified="2015-06-18 17:34:30"> 
    <properties> 
     <field index="1" name="remark"/> 
     <field index="2" name="title">SERIES B</field> 
    </properties> 
    <list> 
     <theme index="1" name="q" type="lb" title="" id="ad703b7e" created="2015-05-28 11:17:09" modified="2015-06-19 11:23:13"> 
      <properties> 
       <field index="1" name="remark"/> 
       <field index="2" name="content">Test</field> 
      </properties> 
      <list> 
       <item index="1" name="q" type="letterbingoitem" title="" id="8a4a5c65" created="2015-05-28 11:19:35" modified="2015-05-28 11:19:35"> 
        <field index="1" name="query">content 3</field> 
        <field index="2" name="remark"/> 
       </item> 
       <item index="2" name="q" type="letterbingoitem" title="" id="597f9791" created="2015-05-28 11:18:00" modified="2015-05-28 11:18:00"> 
        <field index="1" name="query">content 4</field> 
        <field index="2" name="remark"/> 
       </item> 
      </list> 
     </theme> 
    </list> 
    </theme> 
</list> 
</folder> 

我已经缩短了一点的可读性,我只显示每个列表的两个项目,其中居然有几十个项目。

我试图提取主题与索引1和索引2的项目中的字段分开。

对于这个我使用这个XML到LINQ查询:

int indexToSearchFor = 1; 

var temp = xml.Descendants("list").Where(e => (string)e.Attribute("name") == "content5").Descendants("theme").Where(g => (string)g.Attribute("type") == "lb" && (string)g.Attribute("index") == indexToSearchFor.ToString()).Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "query").Select(z => z.Value).ToList(); 

该查询基本上是由我使用另一个查询的修改。

现在,当我运行此查询时,结果列表包含名称为“query”的所有字段,因此内容1,内容2,内容3和内容4在哪里,因为我只用字段查找名称“查询”来自类型为“lb”和索引“1”的主题。

我需要处理主题与索引1和索引2分开,所以在我的情况下,这是行不通的。但我似乎无法弄清楚为什么?

任何人都可以指出我正确的方向吗?

谢谢。

虽然再次阅读了我的文章,我只是发现了错误...

在我的XML两型“磅”的主题具有相同的索引。这是父主题有不同的指标,所以我需要使用这些。所以修改后的代码是:

var temp = xml.Descendants("list").Where(e => (string)e.Attribute("name") == "content5").Descendants("theme").Where(g => (string)g.Attribute("type") == "r1theme" && (string)g.Attribute("index") == indexToSearchFor.ToString()).Descendants("field").Where(x => x.Attribute("name") != null).Where(y => y.Attribute("name").Value == "query").Select(z => z.Value).ToList();