Use XPath to parse XML files with xmlns

Source: Internet
Author: User

In. net, the Program for writing and reading XML prompts "the instance of the object has not been referenced", it was a bit strange at the time. why do we need to instantiate an object when reading XML data. google knows that xmlns is added to the XML file to indicate the namespace, but XPath must also be added.

For example, our XML file is defined:

<Project name = "target plan project"> <process name = "Personal monthly plan" Description = "Personal monthly plan" version = "1"> <node name = "start" Type = "start" Description = "start"> <event> </node> </process> </Project>

 

We only need to read

XmlDocument xmlDoc = new XmlDocument();        xmlDoc.Load(path);         string xPath = @"Project/Process";        XmlNode xn = xmlDoc.SelectSingleNode(xPath);

 

Write in this way

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.xsdWFPD.xsd" name = "target plan project"> <process name = "Personal monthly plan" Description = "Personal monthly plan" version = "1"> <node name =" start "type =" start "Description =" start "> <event> </node> </process> </Project>

 

We must call this method when reading data.

  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);

 

I am confused about this broken problem for one night and finally got it done.

The solution is probably to resolve a simple file without xmlns to prove that your XPath expression is correct. Therefore, the problem lies in the structure difference of XML files. After about N hundred times of experiment, I finally found that the xmlns guy looked strange. Baidu took a look at "XML xmlns" and immediately saw the post and solution to this problem that the predecessors had encountered.

In addition to the last two paragraphs and questions mentioned above, they all refer to other people's articles, because they are clear enough. Using XPath to parse XML files is a fault caused by xmlns

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.