相关疑难解决方法(0)

最好的LINQ-to-XML查询,根据后代节点的属性选择节点?

我有以下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)

c# xml linq-to-xml

3
推荐指数
1
解决办法
1万
查看次数

C#XML,找到节点和他所有的父母

我有一个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)

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));
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表达式,直到它为空,但有没有更好(更清洁)的方法?

c# xml linq

3
推荐指数
1
解决办法
5907
查看次数

标签 统计

c# ×2

xml ×2

linq ×1

linq-to-xml ×1