For a long time did not do XML aspects of parsing, today received a small task, need to parse the XML inside the field content, incidentally reviewed the XPath knowledge. The XML that needs parsing is the kind of document to be namespace, and at the beginning of the parsing, I feel that it is the name space that has dealt with the problem before, but the time is long, forgotten, and not recorded.
Toss it for a while and finally find a way. NET to read XML documents with XML namespaces is really cumbersome and not straightforward enough. Anyway, keep a record of the solution to save you from being tossed around again.
StringReader TextReader = new StringReader (surveyproject.surverycontent); XPathDocument xd = new XPathDocument (TextReader); XPathNavigator navigator = xd. CreateNavigator (); --Join the namespace NameTable nt = new NameTable (); XmlNamespaceManager nsmgr = new XmlNamespaceManager (NT); Nsmgr. AddNamespace ("RC", "http://sechemas.hirede.com/2011/05/Apps/ReferenceChecking"); --When used, it has to be passed xpathnavigator pathnavigator = navigator. selectSingleNode ("//rc:basicinformation/rc:name", nsmgr); if (pathnavigator! = null) { return pathnavigator.value; }
Method memo for reading XML documents with namespaces