c#xmlXPathSelectNodes,SelectSingleNode无法获取

  最近使用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 


当前名称:c#xmlXPathSelectNodes,SelectSingleNode无法获取
当前链接:http://bzwzjz.com/article/pegpog.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 网站制作公司 成都网站设计 重庆手机网站建设 网站设计 网站建设费用 成都网站建设 企业网站制作 高端网站设计 成都网站建设 阿坝网站设计 高端定制网站设计 品牌网站建设 外贸网站建设 网站设计制作 教育网站设计方案 高端网站设计 网站建设公司 企业网站设计 成都网站建设公司 成都营销网站制作 网站建设公司 重庆企业网站建设