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