XPath和XPathSelectElement [英] XPath and XPathSelectElement
本文介绍了XPath和XPathSelectElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下XML
<根和GT;
<数据库>
< DB1名称=名称1/>
< DB2 NAME =名称2服务器=MYSERVER/>
< DB3 NAME =NAME3/>
< /数据库>
<根和GT;
我已经竭尽所能了所有可以读取DB2的名字(=名称2) 。XPath查询的组合,但从来没有得到预期的结果。
到目前为止我的代码:
VAR的查询=根/数据库/ DB2。; //这里我用尽了一切
VAR DOC = XDocument.Load(myconfig.xml);
VAR DBNAME = doc.XPathSelectElement(查询);
什么是正确的查询,让我的名称2(即属性的值)?
感谢您的帮助。
解决方案
<$ 。C $ C> VAR DBNAME = doc.XPathSelectElement(根/数据库/ DB2)属性(名称);
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.
解决方案
var dbName = doc.XPathSelectElement("root/databases/db2").Attribute("name");
这篇关于XPath和XPathSelectElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文