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

StringBuildersb=newStringBuilder();XPathDocumentdoc=newXPathDocument(newStringReader(sw.ToString()));XPathNavigatornav=doc.CreateNavigator();XPathNodeIteratornodes=nav.Select(xpath);while(nodes.MoveNext()){***********}

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

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

StringBuildersb=newStringBuilder();XPathDocumentdoc=newXPathDocument(newStringReader(sw.ToString()));XPathNavigatornav=doc.CreateNavigator();XmlNamespaceManagernsMgr=newXmlNamespaceManager(nav.NameTable);if(strNamespaceURL!=null){nsMgr.AddNamespace("ns",strNamespaceURL);}XPathNodeIteratornodes=nav.Select(xpath,nsMgr);
while(nodes.MoveNext()){********}

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

可参照文章:

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

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