4

If I have the following xml document:

<xml>
<data>
<dataset name="X"></dataset>
</data>
</xml>

How can I use Xpath in c# to retrieve the value of the name attribute (i.e. X)

3 Answers 3

4

How can I use Xpath in c# to retrieve the value of the name attribute (i.e. X)

This XPath expression:

/xml/data/dataset/@name 

selects the wanted attribute -- all atributes named name that belong to a dataset element that is a child of a data element that is a child of the top element of the XML document.

However, you want to get the value of the attribute -- not the node itself.

This XPath expression:

string(/xml/data/dataset/@name) 

when evaluated, produces the wanted string value.

In C# use the XPathNavigator.Evaluate() method to evaluate the expression above.

Sign up to request clarification or add additional context in comments.

Comments

2

Use this XPath:

xml/data/dataset/@name

Comments

0

use this XPath expression:

xml/data/dataset

this will retrieve the dataset node. after that you can use C# tools to retrieve the attribute name from the node.

2 Comments

That ius fine if I know the path but not for dynamic xml content
then your question was misleading. if you only need to find elements which have name attribute in them, use @name path instead of the path i wrote above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.