gpt4 book ai didi

c# - 简单的 XPathNavigator GetAttribute

转载 作者:数据小太阳 更新时间:2023-10-29 01:49:19 27 4
gpt4 key购买 nike

我第一次使用 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 上的文档,但不太了解发生了什么。

最佳答案

这里有两个问题:

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

关于c# - 简单的 XPathNavigator GetAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16370183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com