简体   繁体   English

如何使用LINQ to XML从节点获取值?

[英]How do I get the value from a node using LINQ to XML?

I have this XML file: 我有这个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<aBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <a>
    <a>
      <b>some data</b>
      <c>other data</c>
    </a>
    <a>
      <b>some data</b>
      <c>other data</c>
    </a>
  </a>
</aBase>

I want to get all data from <b> nodes using LINQ to XML. 我想使用LINQ to XML从<b>节点获取所有数据。 I have tried something like this: 我已经尝试过这样的事情:

var query = from c in xmlData.Descendants("b")
            select c.Value;

but it doesn't work. 但这不起作用。

How can I do this? 我怎样才能做到这一点? Also, what is the difference between Descendents, Elements and Nodes? 另外,后代,元素和节点之间有什么区别?

You can write this query to get all information from inner a nodes: 您可以编写此查询以从内部a节点获取所有信息:

var query = from e in doc.Root.Elements( "a" ).Elements( "a" )
            select new { B = e.Element( "b" ).Value, C = e.Element( "c" ).Value };

But this is just a query. 但这只是一个查询。 To execute it and work with results write this code: 要执行它并使用结果,请编写以下代码:

foreach ( var e in query )
{
    // Do something with results... For example, write to console:
    Console.WriteLine( "B: " + e.B + ", C: " + e.C );
}

Node is the one element of the XML tree. 节点是XML树的元素之一。 aBase element, all the a , b and c elements - the nodes of the XML document. aBase元素,所有abc元素aBase文档的节点。

Elements is just the child elements of selected node (in one level of hierarchy). 元素只是所选节点的子元素(在一个层次结构中)。 For example for the aBase node the only child element is the outer a element, not the inner a , b or c element. 例如,对于aBase节点,唯一的子元素是外部a元素,而不是内部abc元素。

Descendants is all the elements that are child or descendants to the current node (in all levels of hierarchy). 后代是当前节点(在所有层次结构中)的子级或后代的所有元素。 All elements are the descendants to the aBase element because it is the main element of the document. 所有元素都是aBase元素的后代,因为它是文档的主要元素。

xmlData.Descendants("b") will give you a collection of XElements with XName equal to "b". xmlData.Descendants("b")将为您提供XName等于“ b”的XElement集合。 You can then iteration over this collection to get every value: 然后,您可以遍历此集合以获取每个值:

var data = xmlData.Descendants("b");
foreach (XElement b in data)
{
 string value = b.Value;
 // do something with value here
}

Descendants will give you any Element that is a descendant, not only the direct children, but also the children of them. Descendants会给您任何属于后代的元素,不仅是直接子代,还包括它们的子代。 So in your case xmlData.Root.Descendants("b") will return a collection over every B element in the file. 因此,在您的情况下, xmlData.Root.Descendants("b")将返回文件中每个B元素的集合。 Elements does the same thing but only for direct descendants and Nodes does the same thing but includes comments and textNodes. Elements执行相同的操作,但仅适用于直接后代,而Nodes执行相同的操作,但包含注释和textNode。

Here you go: 干得好:

XDocument doc = XDocument.Parse(xml);

XNamespace ns = doc.Root.Name.Namespace;
var query = doc.Root.Elements(ns + "a").Elements(ns + "a").Elements().Select(el => el.Value);

Please note that you don't even need neither the Descendants method nor iteration through descendants. 请注意,您甚至不需要Descendants方法或遍历Descendants方法。 Elements without parameters returns direct descendants, and Select method extracts values of descendants. 没有参数的Elements返回直接后代,并且Select方法提取后代的值。 And don't forget about xml namespace or elements names could not be found. 并且不要忘了xml名称空间或找不到元素名称。 Without namespace you're trying to extract different elements, not the elements in your xml. 没有命名空间,您将尝试提取不同的元素,而不是xml中的元素。

As for difference between different methods: 至于不同方法之间的区别:

  1. Nodes() returns IEnumerable<XNode> Nodes()返回IEnumerable<XNode>
  2. Elements() returns IEnumerable<XElement> Elements()返回IEnumerable<XElement>
  3. Descendants() returns all descendant elements (including all deeply nested elements in opposite to Nodes and Elements return only 1st level child elements of calling element. Descendants()返回所有后代元素(包括与Nodes相反的所有深层嵌套元素,而Elements仅返回调用元素的1级子元素。
var query = from c in xmlData.Descendants("b").First()
        select c.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM