我有以下XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<FamilyTree>
<Parent name="Ken">
<Child name="Lorna">
<Grandchild name="Andrew"/>
<Grandchild name="Brian"/>
</Child>
<Child name="Mike">
<Grandchild name="Ann"/>
<Grandchild name="Beth"/>
</Child>
</Parent>
<Parent name="Norma">
<Child name="Owen">
<Grandchild name="Charles"/>
</Child>
<Child name="Peter">
<Grandchild name="Charlotte"/>
</Child>
</Parent>
<Parent name="Quinn">
<Child name="Robert">
<Grandchild name="Debbie"/>
<Grandchild name="Eric"/>
</Child>
<Child name="Susan">
<Grandchild name="Frank"/>
</Child>
</Parent>
<Parent name="Tom">
<Child name="Ursula">
<Grandchild name="George"/>
<Grandchild name="Harriet"/>
</Child>
<Child name="Victor">
<Grandchild name="Ian"/>
<Grandchild name="Juliet"/>
</Child>
</Parent>
</FamilyTree>
Run Code Online (Sandbox Code Playgroud)
我正试图选择所有"父母"和一个至少有两个孩子("孙子")的孩子.请注意,我不是在寻找至少有两个"孙子"的"父母".
以下LINQ查询有效,但我觉得它不是最优雅的.
IEnumerable<XElement> parents = (from c in familyTreeElement.Descendants("Child")
where …Run Code Online (Sandbox Code Playgroud) 我有一个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>
Run Code Online (Sandbox Code Playgroud)
我有这样的事情:
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));
Run Code Online (Sandbox Code Playgroud)
哪个返回我的节点及其父节点.我怎么能不仅获得节点的父节点而且还获得它的"祖父母",我的意思是父节点的父节点等.因此,使用我的XML,它将是:
<siteNode controller="eee" action="fff" title="" />
with parent
<siteNode controller="cc" action="dd" title="" >
with parent
<siteNode controller="a" action="b" title="" >
Run Code Online (Sandbox Code Playgroud)
明显的答案是在找到的父项上使用该linq表达式,直到它为空,但有没有更好(更清洁)的方法?