且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要检索特定的XML节点值

更新时间:2023-01-17 18:02:18

First of all, check your XML for errors. It can be a copy-paste or site rendering issue, but the displayed document has a few errors in the values representing URLs and containing ’&’. Ampersand is a special character and must be encoded as &.



If you want to use XmlDocument, you can try XPath to quickly find the desired element. In this case you have to use a namespace manager, for your elements belong to a non-empty namespace. Below is one of the valid solutions:

First of all, check your XML for errors. It can be a copy-paste or site rendering issue, but the displayed document has a few errors in the values representing URLs and containing '&'. Ampersand is a special character and must be encoded as &.

If you want to use XmlDocument, you can try XPath to quickly find the desired element. In this case you have to use a namespace manager, for your elements belong to a non-empty namespace. Below is one of the valid solutions:
XmlDocument doc = new XmlDocument();
//load your doc
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("m", "http://schemas.microsoft.com/VisualStudio/TeamTest/UITest/2010");
XmlElement el = (XmlElement) doc.SelectSingleNode("//m:UIObject[@Id='vTigerLoginPage']", xnm);



Although XmlDocument is the most popular option, I don’t recommend to use it if you only need to read from a document. The better option would be to use XPathDocument or XDocument (in later versions of .NET).


Although XmlDocument is the most popular option, I don't recommend to use it if you only need to read from a document. The better option would be to use XPathDocument or XDocument (in later versions of .NET).