2015-06-20 67 views
0

我開始理解這個整個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分開,所以在我的情況下,這是行不通的。但我似乎無法弄清楚爲什麼?

任何人都可以指出我正確的方向嗎?

謝謝。

回答

0

雖然再次閱讀了我的文章,我只是發現了錯誤...

在我的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();