XPath expression to select all nodes - c#

I would like to find all <Field /> nodes (that may be arbitrarily nested) inside a given XmlNode.
If do something like this:
foreach(XmlNode n in node.SelectNodes('//Field'))...
This returns all nodes in the entire document, not all nodes under node.
Is this how XPath is supposed to work? I looked at some documents and it seems like the //Node query should be scoped to whatever node it's invoked at.
Is there any other technique to select all nodes with a given name that are under a specific node?

If you use '//Field' it's absolut from the root of the document. To search relative to the current node, just use './/Field'.

Use ./Field.
.// Means descendants, which includes children of children (and so forth).
./ Means direct children.
If a XPath starts with a / it becomes relative to the root of the document; to make it relative to your own node start it with ./.

try to use SelecteSingleNode()

Remove // because otherwise it search among all the document irrelatively to the root node.
node.SelectNodes("Field")

You can use simple linq query like this:
var techLeads = (from value in element.Descendants ("Manager")
where value.Attribute ("Name").Value == "Mgr1"
select value).Descendants("TechLead");
Sample Xml:
<Employees>
<Manager Name="Mgr1">
<TechLead Name="TL1" />
<TechLead Name="TL2" />
</Manager>
</Employees>

Related

Select a XML node based on a sibling node

I'm trying to select an XML node, where another child of the parent node contains a specific value.
The XML looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT>
<Objects>
<Media>don't care</Media>
<Style>don't care</Style>
<Book>don't care</Book>
<Topic>don't care</Topic>
<Topic>
<Object>
<Description>Performance Evidence</Description>
</Object>
<Text>This is what I want to select</Text>
</Topic>
</Objects>
</AuthorIT>
I'm using XPath in C#. My query at the moment looks like this: (but doesn't work, obviously)
docNav = new XPathDocument(localFile);
nav = docNav.CreateNavigator();
xPath = "//Topic[Object/Description = 'Performance Evidence']/Text";
string value = nav.SelectSingleNode(xPath).Value;
How do I get the contents of the Text node, from the Topic that has an Object/Description value of "Performance Evidence"?
You should first select the Description node containing your needle text, then move back to the common parent and select the Text nodes that are its children.
//Topic/Object/Description[text()='Performance Evidence']/../../Text/text()
As Kirill Polishchuk said in a comment, my XPath was correct.
What I left out of the example XML was the key to the solution... Namespace!
I found my answer on this other question: Using Xpath With Default Namespace in C#

C# Get all nodes in xml document but ignoring nested nodes

I have an xml document where i need to iterate over all nodes that are direct descendants of the parent.
For example i have the following xml document
<root>
<node1>val1</node1>
<node2>val2</node2>
<nodes>
<nestedNode>nestedvalue</nestedNode>
</nodes>
</root>
I have the following code which gets me all the elements:
XmlNodeList nodes = doc.SelectNodes("//*");
This returns node1, node2, and nestedNode. What i want is only node1 and node2 and to ignore any nested values.
Thanks in advance for any help.
To select elements that are children of the root element you would use the xpath:
/root/*
or in general:
/*/*
You should not traverse the all descendants here (//...) as that will go through all elements in the document. You would have to add additional filtering which would make the query unnecessarily complicated:
//*[parent::*[not(parent::*)]]
However, you want to filter out elements that do not have other child elements so you need to add the condition not(*):
/*/*[not(*)]

Get specific data from XML document

I have xml document like this:
<level1>
<level2>
<level3>
<attribute1>...</attribute1>
<attribute2>false</attribute2>
<attribute3>...</attribute3>
</level3>
<level3>
<attribute1>...</attribute1>
<attribute2>true</attribute2>
<attribute3>...</attribute3>
</level3>
</level2>
<level2>
<level3>
<attribute1>...</attribute1>
<attribute2>false</attribute2>
...
...
...
I'm using c#, and I want to go thru all "level3", and for every "level3", i want to read attribute2, and if it says "true", i want to print the corresponding attribute3 (can be "level3" without these attributes).
I keep the xml in XmlDocument.
Then I keep all the "level3" nodes like this:
XmlNodeList xnList = document.SelectNodes(String.Format("/level1/level2/level3"));
(document is the XmlDocument).
But from now on, I don't know exactly how to continue. I tried going thru xnList with for..each, but nothing works fine for me..
How can I do it?
Thanks a lot
Well I'd use LINQ to XML:
var results = from level3 in doc.Descendants("level3")
where (bool) level3.Element("attribute2")
select level3.Element("attribute3").Value;
foreach (string result in results)
{
Console.WriteLine(result);
}
LINQ to XML makes all kinds of things much simpler than the XmlDocument API. Of course, the downside is that it requires .NET 3.5...
(By the way, naming elements attributeN is a bit confusing... one would expect attribute to refer to an actual XML attribute...)
You can use LINQ to XML and reading this is a good start.
You can use an XPath query. This will give you a XmlNodeList that contains all <attribute3> elements that match your requirement:
var list = document.SelectNodes("//level3[attribute2 = 'true']/attribute3");
foreach(XmlNode node in list)
{
Console.WriteLine(node.InnerText);
}
You can split the above xpath query in three parts:
"//level3" queries for all descendant elements named <level3>.
"[attribute2 = 'true']" filters the result from (1) and only keeps the elements where the child element <attribute2> contains the text true.
"/attribute3" takes the <attribute3> childnode of each element in the result of (2).

How to get the immediate child elements of the root element using C# and XML?

<Document>
<Heading1>
<text>Heading Title</text>
<para>para1</para>
<para>para2</para>
<para>para3</para>
</Heading1>
<Heading1>
<text>2nd Heading Title</text>
<para>para4</para>
<para>para5</para>
<para>para6</para>
<Heading2>
<text>3rd Heading Title</text>
<para>para4</para>
<para>para5</para>
</Heading2>
</Heading1>
</Document>
This is XML Document. Now, i want to parse this XML file using C# (4.0). Here, I want to get all the Heading1 elements without using that element name in my program. For example, don't use document.GetElementsByTagName("Heading1");. How i get it. Guide me get out of this issue.
Thanks & Regards.
Using LINQ to XML, you can do:
var headings = yourXDocument.Root.Elements();
Using Nodes() instead of Elements() will also return text nodes and comments, which is apparently not what you want.
You can access the child elements of the document or element through the Elements() method if using LINQ to XML.
XDocument doc = ...;
var query = doc.Root.Elements();
If you're using XmlDocument, this works:
var elements = doc.SelectNodes("/*/*");
That finds all child elements of the top-level element irrespective of any of their names. It's usually safer to specify the names if you know them, so that elements with unexpected names don't get returned in your list - use /Document/Heading1 to do this.

Relative XPath selection using XmlNode (c#)

Say i have the following xml file:
<a>
<b>
<c></c>
</b>
<b>
<c></c>
</b>
</a>
var nodes = doc.SelectNodes("/a/b");
will select the two b nodes.
I then loop these two nodes suchas:
foreach (XmlNode node in nodes) { }
However, when i call node.SelectNodes("/a/b/c"); It still returns both values and not just the descendants.
Is it possible to select nodes only descening from the current node?
In the foreach loop, you already know that node is a /a/b in the original document - so to get just its c children simply use a relative xpath:
node.SelectNodes("c")
You can use node.SelectSingleNode("C");
/a/b[1]/c
For intance gets the nodelist of all the children of the first b that have the tagname c.
To get the first c as a singleton nodelist use /a/b[1]/c[1]. /a/b/c[1] again returns a nodelist of multiple nodes.
SelectSingleNode is probably misleading, as far as I know, XPath always returns a nodelist, which can contain optionally one node (or even be empty).
//c[1] just selects the first c in the document.

Categories

Resources