I'd like to be able to evaluate normal XPath expressions against the following XML which has a pesky xmlns attribute that I don't care about:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="AuthorIT.xslt"?>
<AuthorIT version="6.0.8" xmlns="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
<Objects>
<Book>
<Object>
<CreatedBy>Me</CreatedBy>
<CreatedDate>2012-11-20T12:35:33</CreatedDate>
<Description>String I want to get</Description>
<FolderID>12345</FolderID>
<GUID>abcdefg1234567abcdefg1234567abcd</GUID>
<ID>99999</ID>
</Object>
</Book>
</Objects>
</AuthorIT>
In XMLSpy (or this free web tool) the expression to the "String I want to get" is as follows:
/AuthorIT/Objects/Book/Object/Description
This is how I'm specifying the namespace in c#:
XPathDocument document = new XPathDocument("/path/to/my/file");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("abc", "http://www.authorit.com/xml/authorit");
Once I've specified the namespace, here's how I have to amend the perfectly-good XPath expression to get the same result:
/abc:AuthorIT/abc:Objects/abc:Book/abc:Object/abc:Description
Horrible, ain't it?! Is there a way to evaluate XPath expressions that bypass and totally ignore the presence of this extremely pesky "xmlns" attribute on the AuthorIT root element? If I totally strip the root element so that it's just by itself, I don't need to specify a namespace and I can use normal expressions. Is there a way to tell .NET to just work as if the "xmlns" rubbish wasn't there in the first place?
tag, etc, is getting an xmlns="rubbish" attribute, which I would consider to be a very nasty bug/side-effect. Perhaps it's the .NET Framework, I don't know. All my problems have now gone, however, just by removing this pointless attribute from the root node :-)
– Rainier Wolfcastle Aug 16 '13 at 01:26