这只是我在XPathNavigator的第一次拍摄的开始。
这是我的简单xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<theroot>
<thisnode>
<thiselement visible="true" dosomething="false"/>
<another closed node />
</thisnode>
</theroot>现在,我使用CommonLibrary.NET库来帮我一点忙:
public static XmlDocument theXML = XmlUtils.LoadXMLFromFile(PathToXMLFile);
const string thexpath = "/theroot/thisnode";
public static void test() {
XPathNavigator xpn = theXML.CreateNavigator();
xpn.Select(thexpath);
string thisstring = xpn.GetAttribute("visible","");
System.Windows.Forms.MessageBox.Show(thisstring);
}问题是它找不到属性。我已经阅读了MSDN上的文档,但无法理解发生了什么。
发布于 2013-05-04 11:12:42
这里有两个问题:
(1)您的路径是选择thisnode元素,但thiselement元素具有和属性
(2) .Select()不改变XPathNavigator的位置。它返回一个包含匹配项的XPathNodeIterator。
试试这个:
public static XmlDocument theXML = XmlUtils.LoadXMLFromFile(PathToXMLFile);
const string thexpath = "/theroot/thisnode/thiselement";
public static void test() {
XPathNavigator xpn = theXML.CreateNavigator();
XPathNavigator thisEl = xpn.SelectSingleNode(thexpath);
string thisstring = xpn.GetAttribute("visible","");
System.Windows.Forms.MessageBox.Show(thisstring);
}发布于 2016-02-13 20:04:37
您可以像这样使用xpath在元素上选择属性(这是上面接受的答案的另一种选择):
public static XmlDocument theXML = XmlUtils.LoadXMLFromFile(PathToXMLFile);
const string thexpath = "/theroot/thisnode/thiselement/@visible";
public static void test() {
XPathNavigator xpn = theXML.CreateNavigator();
XPathNavigator thisAttrib = xpn.SelectSingleNode(thexpath);
string thisstring = thisAttrib.Value;
System.Windows.Forms.MessageBox.Show(thisstring);
}https://stackoverflow.com/questions/16370183
复制相似问题