次のようなXML構造を取得しました。
<siteNode controller="a" action="b" title="">
<siteNode controller="aa" action="bb" title="" />
<siteNode controller="cc" action="dd" title="">
<siteNode controller="eee" action="fff" title="" />
</siteNode>
</siteNode>
C#LinqからXMLまで 、子が条件を満たすときに親を取得する
私はこのようなものを手に入れました:
XElement doc = XElement.Load("path");
var result = doc.Elements("siteNode").Where(parent =>
parent.Elements("siteNode").Any(child => child.Attribute("action").Value ==
ActionName && child.Attribute("controller").Value == ControlerName));
これは私のノードとその親を返します。ノードの親だけでなく、その「祖父母」、つまり親の親などを取得するにはどうすればよいでしょうか。つまり、私のXMLでは次のようになります。
<siteNode controller="eee" action="fff" title="" />
with parent
<siteNode controller="cc" action="dd" title="" >
with parent
<siteNode controller="a" action="b" title="" >
明らかな答えは、見つかった親に対してそのlinq式をnullになるまで使用することですが、より良い(よりクリーンな)方法はありますか?