XPath and XPathSelectElement

2019-02-06 01:56发布

问题:

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.

回答1:

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


回答2:

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"


回答3:

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);