使用Linq和XDocument,我可以获取父标记下的所有子元素吗?

时间:2021-02-03 20:34:27

I have an XML

我有一个XML

<data>
  <summary>
    <account curr_desc='USD' acct_nbr='123' net='1000.00' />
    <account curr_desc='USD' acct_nbr='456' net='2000.00' />
  </summary>
  <details>
    <accounts>
      <account acct_nbr="123" curr="USD">
        <activity color='False' settle_date='02 Jul 2010' amt='580.00' />
        <activity color='True' settle_date='09 Jul 2010' amt='420.00' />
      </account>
      <account acct_nbr="456" curr="USD">
        <activity color='True' settle_date='12 Dec 2010' amt='1500.00' />
        <activity color='True' settle_date='19 Dec 2010' amt='500.00' />
      </account>
    </accounts>
  </details>
</data>

Using Linq and XDocument, I can extract "summary" information but how can I extract "account" information under "summary" tag?

使用Linq和XDocument,我可以提取“摘要”信息,但如何在“摘要”标签下提取“帐户”信息?

XDocument XMLDoc = XDocument.Load("testdata.xml");
XElement accounts = (from xml2 in XMLDoc.Descendants("summary")
                    select xml2).FirstOrDefault();

How can I specify something like "summary/account" so that it returns me all the elements under <summary>? Note, that I have <account> under <detail><accounts>, I only want the elements under summary tag.

如何指定“摘要/帐户”之类的内容,以便它返回

下的所有元素?请注意,我在 下有 ,我只想要摘要标记下的元素。

Edit: (Solution from Andrew)

编辑:(安德鲁的解决方案)

var accounts = XMLDoc.Root.Elements("summary").Elements("account");
foreach (XElement elem in accounts)
{
    sb.Append(elem.ToString());
}

2 个解决方案

#1


18  

You should use the Elements method:

您应该使用Elements方法:

var accounts = doc.Root.Elements("summary").Elements("account");

Or, alternatively, XPathSelectElements, which in this case is simpler:

或者,替代地,XPathSelectElements,在这种情况下更简单:

var accounts = doc.XPathSelectElements("/data/summary/account");

In this instance you can also use Descendants, as Andrew Barber suggested, but in general you should only do this when you really want to find all descendants with a given name, and not just immediate children. Otherwise your code does a lot of searching that it doesn't need to, and may return elements you don't want it to.

在这种情况下,您也可以使用Descendants,正如Andrew Barber建议的那样,但一般情况下,只有在您真正想要查找具有给定名称的所有后代而不仅仅是直接子项时,才应该执行此操作。否则,您的代码会执行大量不需要的搜索,并且可能返回您不希望它的元素。

#2


3  

var accountSummaryElems = 
   XMLDoc.Element("summary").Elements("account");

This gives you a collection of the account elements under the summary element. You can then iterate them to get the values.

这为您提供了摘要元素下的帐户元素的集合。然后,您可以迭代它们以获取值。

EDITED to use the same pattern you were; I call First() instead of FirstOrDefault() because that code won't run anyway if the "account" element is not found.

编辑使用相同的模式;我调用First()而不是FirstOrDefault(),因为如果找不到“account”元素,代码将无法运行。

Then you have the right idea with iterating over the collection returned.

然后你有正确的想法迭代返回的集合。

#1


18  

You should use the Elements method:

您应该使用Elements方法:

var accounts = doc.Root.Elements("summary").Elements("account");

Or, alternatively, XPathSelectElements, which in this case is simpler:

或者,替代地,XPathSelectElements,在这种情况下更简单:

var accounts = doc.XPathSelectElements("/data/summary/account");

In this instance you can also use Descendants, as Andrew Barber suggested, but in general you should only do this when you really want to find all descendants with a given name, and not just immediate children. Otherwise your code does a lot of searching that it doesn't need to, and may return elements you don't want it to.

在这种情况下,您也可以使用Descendants,正如Andrew Barber建议的那样,但一般情况下,只有在您真正想要查找具有给定名称的所有后代而不仅仅是直接子项时,才应该执行此操作。否则,您的代码会执行大量不需要的搜索,并且可能返回您不希望它的元素。

#2


3  

var accountSummaryElems = 
   XMLDoc.Element("summary").Elements("account");

This gives you a collection of the account elements under the summary element. You can then iterate them to get the values.

这为您提供了摘要元素下的帐户元素的集合。然后,您可以迭代它们以获取值。

EDITED to use the same pattern you were; I call First() instead of FirstOrDefault() because that code won't run anyway if the "account" element is not found.

编辑使用相同的模式;我调用First()而不是FirstOrDefault(),因为如果找不到“account”元素,代码将无法运行。

Then you have the right idea with iterating over the collection returned.

然后你有正确的想法迭代返回的集合。