2011-12-04 17 views
0

下記のxmlデータを見つけてください。親ノードにurl = "#"、title = "item 1"、key = "Item1"があることを前提として、LINQクエリを使用してキー "Sub1"と "Sub2"を持つノードを取得したいとします。LINQ XML親ノードの子ノードを取得する

<root> 
<node title="Partner" url="" description="99" roles="GuidIdHere"></node> 
<node title="Test" url="" description="51" roles="8b0c0c4f" key="sample"> 
    <node url="#" title="item 1" description="" key="Item1"> 
     <node url="#" title="Sub 1" description="" key="Sub1" /> 
     <node url="#" title="Sub 2" description="" key="Sub2" /> 
    </node> 
    <node url="#" title="item 2" description="" key="Item2" /> 
    <node url="#" title="item 3" description="" key="Item3" /> 
</node> 
</root> 

ありがとう!

答えて

0
var result = from x in doc.Root.Descendents("node") 
      where (string)x.Attribute("url") == "#" 
       && (string)x.Attribute("title") == "item 1" 
       && (string)x.Attribute("key") == "Item1" 
      from y in x.Elements("node") 
      where (string)x.Attribute("key") == "Sub1" 
       || (string)x.Attribute("key") == "Sub2" 
      select y; 
+0

doc.Root.Descendantsをdoc.Descendantsに変更するだけでしたが、これは完璧でした。ありがとう! – Ronald

関連する問題