Selectnodes,selectsinglenode always returns null
Original address: http://www.cnblogs.com/linlf03/archive/2011/11/30/2268705.html
The following is an example of a simple xml:
requestId="13898" status="1"> <error>Invalid Login</error> </message>
The following attempts to read the contents of the error node
XmlNode errorNode = xmldoc.SelectSingleNode("/message/error"); if (errorNode != null) Console.Writeline("There is an error");
The returned result is always null
The problem arises because namespaces are used in the XML document above, and when namespaces are defined in XML, the following methods are used when locating nodes
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmldoc.NameTable); nsMgr.AddNamespace("ns", "http://www.mydomain.com/MyDataFeed");
XmlNode errorNode = xmldoc.SelectSingleNode("/ns:message/ns:error", nsMgr);
If you want to locate the error directly, rather than starting from the root, you need to write
Thank you: Http://stackoverflow.com/questions/1766254/selectsinglenode-always-returns-null
C # Operations XML Selectnodes,selectsinglenode always returns null