心得:use XPath (+namespace)# DotNet - 窗口里的风景
c*t
1 楼
因为工作的需要,要parse非常复杂的xml.用donet来parse XML非常方便,尤其用XPath
直接选取node(s)。但是当xml比较复杂的时候,比如从crystal reports直接导出的xml
,通常的办法:
XmlDocument xml = new XmlDocument();
xml.Load( PathToXmlFile );
XmlNodeList selection = xml.SelectNodes(strXPathExpression);
就行不通:明明xml.innerXml不空,selection.Count总是0。去
掉namespace就可以了。经过一番Google,终于找到答案:在SelectNodes前加上
XmlNamespaceManager nsmgr = new XmlNamespanceManager(xml.NameTable);
nsmgr.AddNamespace("a", "http://....");
nsmgr.AddNamespace("b", "urn:....");
XmlNodeList selection =
直接选取node(s)。但是当xml比较复杂的时候,比如从crystal reports直接导出的xml
,通常的办法:
XmlDocument xml = new XmlDocument();
xml.Load( PathToXmlFile );
XmlNodeList selection = xml.SelectNodes(strXPathExpression);
就行不通:明明xml.innerXml不空,selection.Count总是0。去
掉namespace就可以了。经过一番Google,终于找到答案:在SelectNodes前加上
XmlNamespaceManager nsmgr = new XmlNamespanceManager(xml.NameTable);
nsmgr.AddNamespace("a", "http://....");
nsmgr.AddNamespace("b", "urn:....");
XmlNodeList selection =