i am trying to create an application which would output the data present at some xpath(which will be specified by user)
XPathDocument xmldoc = new XPathDocument(file);
XPathNavigator nav = xmldoc.CreateNavigator();
XPathNavigator result = nav.SelectSingleNode("//p");
MessageBox.Show(result.Value);
Here variable file is location of xml file.
Now when I am running this code on a xml file that has lots of namespaces defined on it , the above code returns nullreference exception, because variable result is null and i am trying to access
result.Value .
But when i created my own xml file
<a>
<b>
<p>abc</p>
</b>
</a>
the codes runs fine .
So, what i am inferring is that the problem is because i am not including the namespaces in the code.
I searched and found out the suggestion that the way to trick namespaces is to use relative xpaths such as //p .Here What is a good way to find a specific value in an XML document using C#?
But the code still does not works on the original file (one containing the namespaces)