温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c# xml XPath SelectNodes, SelectSingleNode 无法获取

发布时间:2020-06-20 10:35:06 来源:网络 阅读:1755 作者:mathed001 栏目:编程语言

  最近使用Sgml组件,在使用XPath语句获取Notes时,总是无法查找节点,却能找属性值类似"//@alt",

 

  1. StringBuilder sb = new StringBuilder(); 
  2.                     XPathDocument doc = new XPathDocument(new StringReader(sw.ToString())); 
  3.                     XPathNavigator nav = doc.CreateNavigator(); 
  4.                     XPathNodeIterator nodes = nav.Select(xpath); 
  5.                     while (nodes.MoveNext()) 
  6.                     { 
  7.                         *********** 
  8.                     } 

 

结果发现原因就在于上面的xml文档中使用了命名空间,当xml中定义了命名空间时,在查找节点的时候需要使用下面的方法:

参数 =》 strNamespaceURL = “//ns:body”;

  1. StringBuilder sb = new StringBuilder(); 
  2. XPathDocument doc = new XPathDocument(new StringReader(sw.ToString())); 
  3. XPathNavigator nav = doc.CreateNavigator(); 
  4.  
  5. XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable); 
  6. if (strNamespaceURL != null
  7.     nsMgr.AddNamespace("ns", strNamespaceURL); 
  8. XPathNodeIterator nodes = nav.Select(xpath, nsMgr); 

  9. while (nodes.MoveNext()) 
  10.     ******** 

注意添加的命名空间名:ns也是区分大小写的

 

可参照文章:

http://www.cnblogs.com/linlf03/archive/2011/11/30/2268705.html

http://developer.51cto.com/art/200908/144652.htm 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI