简易的XPathNavigator的getAttribute [英] Easy XPathNavigator GetAttribute

查看:349
本文介绍了简易的XPathNavigator的getAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始我的第一个起飞这里开始在的XPathNavigator

Just getting started here with my first take at XPathNavigator.

这是我的简单的XML:

This is my simple xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<theroot>
    <thisnode>
        <thiselement visible="true" dosomething="false"/>
        <another closed node />
    </thisnode>

</theroot>

现在,我使用的是 CommonLibrary.NET 库,帮助我一点:

Now, I am using the CommonLibrary.NET library to help me a little:

    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在此文件看,但不能使所发生的事情的多大意义。

Problem is that it can't find the attribute. I've looked through the documentation at MSDN for this, but can't make much sense of what's happening.

推荐答案

两个问题这里是(1)您的路径选择 thisnode 元素,但 thiselement 元素是一个具有属性和(2)。选择()不会改变的的XPathNavigator 的位置。它返回一个声明XPathNodeIterator 通过匹配

Two problems here are (1) Your path is selecting the thisnode element, but the thiselement element is the one with the attributes and (2) .Select() does not change the location of the XPathNavigator. It returns an XPathNodeIterator with the matches.

尝试这样的:

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

这篇关于简易的XPathNavigator的getAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆