I have an XML-File, which has the following structure:
<mediawiki ...>
<siteinfo>...</siteinfo>
<page>
<title>The Title</title>
<id>42</id>
...
</page>
... more page items ...
</mediawiki>
I red a bit about XPath-Query and wrote the following code:
_xmlArticlesDocument = new XmlDocument();
_xmlArticlesDocument.Load(xmlArticlesFileName);
Now I want to get a grip on a page Element with a given id-Subelement. So I wrote:
XmlNode node = _xmlArticlesDocument.DocumentElement.SelectSingleNode
("//mediawiki/page[id=" + id.ToString() + "]");
but node is always null. I have tried several querys, including "page/id", "page", "/page", "//page" to get anything, but node is always null. I have checked via quick inspection the _xmlArticlesDocument variable and it contains the correct XML-file with the expected structure.
It seems to me, that I have missed something very basic, but have no idea what. Maybe someone here has an idea?
Thanks in advance, Frank
The query looks right on the basis of what you shown us so far. However I suspect behin the "..." on the mediawiki element there is a xmlns="...." attribute right?
That'll be what is tripping you up I suspect. You will need code like this:-
XmlNamespaceManager nsmgr = new XmlNamespaceManager(_xmlArticlesDocument.NameTable);
nsmgr.AddNamespace("a", "the-namespace-in-them-xmlns-attribute");
XmlNode page = _xmlArticlesDocument.SelectSingleNode(String.Format("//a:page[id={0}]", id), nsmgr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With