C # Operations XML Selectnodes,selectsinglenode always returns null

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.