Csharp/C#教程:Easy XPathNavigator GetAttribute分享


Easy XPathNavigator GetAttribute

刚刚开始我的第一次参加XPathNavigator

这是我的简单xml:

        

现在,我使用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); } 

您可以像这样使用xpath选择元素的属性(上面接受的答案的替代方法):

上述就是C#学习教程:Easy XPathNavigator GetAttribute分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

 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://www.ctvol.com/cdevelopment/960991.html

(0)
上一篇 2021年11月25日
下一篇 2021年11月25日

精彩推荐