的LINQ to XML:我怎么得到的XElement的唯一的直接后裔?后裔、直接、我怎么、to

2023-09-04 07:18:15 作者:坚持多久

Dim xml = <Root>
            <Parent id="1">
              <Child>Thomas</Child>
            </Parent>
            <Parent id="2">
              <Child>Tim</Child>
              <Child>Jamie</Child>
            </Parent>
          </Root>

Dim parents = xml.Elements

在这种情况下,孩子包括所有的父元素,所有的子元素。什么是抢的只有直接的后裔最好的办法&LT;根和GT;

In this case, children includes all the Parent elements and all of the Child elements. What's the best way to grab only the direct descendants of <Root>?

我应该写一个LINQ查询选择元素,其中母公司= &LT;根和GT; ?或者是有一些内置的方法,我很想念,可以让这对我?

Should I write a LINQ query that selects elements where parent = <Root>? Or is there some built-in method I'm missing that can get this for me?

编辑:我的一些混乱 XElement.Elements XElement.Descendants 。由于鲁本Bartelink指出, XElement.Elements 会给我正是我所期待的。

I had some confusion between XElement.Elements and XElement.Descendants. As Ruben Bartelink pointed out, XElement.Elements will give me exactly what I was looking for.

谢谢!

推荐答案

Exec的总结 - 你想要的:

Exec summary - you want:

xml.Elements.Select(function(element) new XElement(element.Name,element.Attributes))

第一个答案:

First answer:

XElement.Descendants ,或者是一个有趣的问题? :P There's后人在这里的使用

XElement.Descendants, or is it a trick question ? :P There's an example of usage of Descendants here

修订的答案,谢谢托玛 - 什么事情也觉得不对劲:

Revised answer, thanks Tormod -- something did feel wrong!:

元素可以直接后裔,因为你要寻找的。后人提供了完整的层次[因为你是指控的元素呢。 (这个例子我联系到了这一点,道歉的混乱!

Elements gives direct descendants, as you're looking for. Descendants gives the full hierarchy [as you are alleging Elements does]. (The example I linked to makes this clear. Apologies for the confusion!

所以,最后,你要找的是什么(这个时候在VB中):

So, finally, what you're looking for is (this time in VB):

Dim xml = <Root>
        <Parent id="1">
          <Child>Thomas</Child>
        </Parent>
        <Parent id="2">
          <Child>Tim</Child>
          <Child>Jamie</Child>
        </Parent>
      </Root>

REM All nodes two levels down in the hierarchy
Dim level2Nodes = xml.Elements.SelectMany(function(element) element.Elements)
level2Nodes.Dump

REM All Child nodes, no matter where they are:
Dim children = xml.Descendants("Child")

每个将产生你的3```元素,如包括在REMS不同的原因。

Each of which will yield you the 3 ``` elements for different reasons as covered in the REMs.

(粘贴上面直接进入 LINQPad 在VB中的语句模式)

(Paste the above directly into LINQPad in VB statement mode)

我现在看到的可能混淆你 - 当你使用的元素和看看它的Visualiser的,你仍然看到孩子们: -

I now see what might be confusing you - when you use Elements and look at it in a visualiser, you are still seeing the children:-

Dim parents = xml.Elements

如果您只想实际的名称,可以使用类似:

If you only want the actual names, you can use something like:

Dim parentNames = xml.Elements.Select(function(element) element.Name)

请注意,在每一种情况下,你得到两个结果

Note that in each of these cases, you are getting two results.

如果你真的想带出chidren,你想要的:

If you really want to strip out the chidren, you want:

Dim parentElements = xml.Elements.Select(function(element) new XElement(element.Name,element.Attributes))

您可以延长你的问题,以显示你真的想要?

Can you extend your question to show what you're really looking for?