gsharp
gsharp

Reputation: 27927

XPath and XPathSelectElement

I have the following xml

<root>
   <databases>
      <db1 name="Name1" />
      <db2 name="Name2" server="myserver" />
      <db3 name="Name3" />
   </databases>
<root>

I've tried everything to read the name of the db2 (="Name2") with all possible combinations of XPath queries, but never get the expected result.

My Code so far:

var query = "root/databases/db2.. "; // here I've tried everything 
var doc = XDocument.Load("myconfig.xml");
var dbName =  doc.XPathSelectElement(query);

What's the correct query to get my "Name2" (the value of the Attribute) ?

Thanks for your help.

Upvotes: 20

Views: 50568

Answers (3)

dtb
dtb

Reputation: 217293

The XPathSelectElement method can only be used to select elements, not attributes.

For attributes, you need to use the more general XPathEvaluate method:

var result = ((IEnumerable<object>)doc.XPathEvaluate("root/databases/db2/@name"))
                                      .OfType<XAttribute>()
                                      .Single()
                                      .Value;

// result == "Name2"

Upvotes: 38

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59111

To get the value (Name2) of the name attribute, of the db2 element, try this:

    var query = "root/databases/db2";
    var doc = XDocument.Load("myconfig.xml");
    var dbElement = doc.XPathSelectElement(query);
    Console.WriteLine(dbElement.Attribute("name").Value);

If you don't know the name of the element (db2), but do know that it has a server attribute, try this:

    var query = "root/databases/*[@server]";
    var doc = XDocument.Load("myconfig.xml");
    var dbElement = doc.XPathSelectElement(query);
    Console.WriteLine(dbElement.Attribute("name").Value);

If you want to do the same thing as the previous example, but there are multiple elements with a server attribute, and you want to pick between those, try this:

    var query = "root/databases/*[@server='myserver']";
    var doc = XDocument.Load("myconfig.xml");
    var dbElement = doc.XPathSelectElement(query);
    Console.WriteLine(dbElement.Attribute("name").Value);

Upvotes: 17

Alex Aza
Alex Aza

Reputation: 78457

var dbName = doc.XPathSelectElement("root/databases/db2").Attribute("name");

Upvotes: 23

Related Questions