加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Easy XPathNavigator GetAttribute

发布时间:2020-12-15 17:50:18 所属栏目:百科 来源:网络整理
导读:刚刚开始我的第一次参加XPathNavigator. 这是我的简单xml: ?xml version="1.0" encoding="utf-8" standalone="yes"?theroot thisnode thiselement visible="true" dosomething="false"/ another closed node / /thisnode/theroot 现在,我使用CommonLibrary.
刚刚开始我的第一次参加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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读