XPath and XPathSelectElement

醉酒当歌 提交于 2019-12-30 02:48:11

问题


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


来源:https://stackoverflow.com/questions/6289784/xpath-and-xpathselectelement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!