XML parsing error xmlns

Source: Internet
Author: User

In. NET, it was a bit odd to read the prompts in the program that read the XML to "not set an object reference to an instance of an object." Why you should instantiate an object while reading the XML data. Google did not know that the XML file included the xmlns representation namespace, But XPath must also be added.

As previously our XML file is defined as:

<project name= "Target planning Project" >
<process name= "Personal monthly Plan" description= "personal monthly Plan" version= "1" >
<node name= "Start" type= "Start" description= "Begin" >
<Event></Event>
</Node>

</Process>

</Project>

We read just
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (path);

String xPath = @ "Project/process";
XmlNode xn = xmldoc.selectsinglenode (XPath);

This writing

If XML is defined like this

<project xmlns= "http://tempuri.org/XMLSchema.xsd" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: Schemalocation= "Http://tempuri.org/XMLSchema.xsd
Wfpd.xsd "name=" target program >
<process name= "Personal monthly Plan" description= "personal monthly Plan" version= "1" >
<node name= "Start" type= "Start" description= "Begin" >
<Event></Event>
</Node>

</Process>

</Project>

We must call this when we read it.

XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (path);

XmlNamespaceManager xnm = new XmlNamespaceManager (xmldoc.nametable);
Xnm. AddNamespace ("Mxh", "http://tempuri.org/XMLSchema.xsd");
String xPath = @ "/mxh:project/mxh:process";
XmlNode xn = xmldoc.selectsinglenode (XPath, xnm);

Ok! 's got a lot of insight.

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.