I have this XML file:
<?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.
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:
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. aBase
element, all the a
, b
and c
elements - the nodes of the XML document.
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.
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.
xmlData.Descendants("b")
will give you a collection of XElements with XName equal to "b". 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. So in your case xmlData.Root.Descendants("b")
will return a collection over every B element in the file. Elements
does the same thing but only for direct descendants and Nodes
does the same thing but includes comments and textNodes.
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. Elements
without parameters returns direct descendants, and Select
method extracts values of descendants. And don't forget about xml namespace or elements names could not be found. Without namespace you're trying to extract different elements, not the elements in your xml.
As for difference between different methods:
Nodes()
returnsIEnumerable<XNode>
Elements()
returnsIEnumerable<XElement>
Descendants()
returns all descendant elements (including all deeply nested elements in opposite toNodes
andElements
return only 1st level child elements of calling element.
var query = from c in xmlData.Descendants("b").First()
select c.Value;
精彩评论