tags:

views:

183

answers:

2

I'm using .Net 2.0, and need to SelectSingleNode from my XmlDocument regardless of namespace, as wrong headed as that may sound.

to be specific

XmlElement slipType = (XmlElement)document.SelectSingleNode("//Provenance1");

will set slipType to null since I don'l know th namespace Provenance1 is in at the time of the query.

A: 

Try:

XmlElement slipType = (XmlElement)document.SelectSingleNode("//*:Provenance1");

Or:

XmlElement slipType = (XmlElement)document.SelectSingleNode("//@*:Provenance1");

for attributes...

Unfortunately, this construction would only work with XPath 2.0, while .NET uses only XPath 1.0. I accidently tested above code with a 2.0 parser, so it doesn't work.)

Workshop Alex
This throws an XPathException //*:Provenance1 has an invalid token.
Ralph Shillington
I've tested it with .NET 3.5, where it crashed too. However, it is a valid XPath construction. Unfortunately for XPath 2.0, which I had tested. But .NET uses only XPath 1.0 and it's unlikely .NET will support 2.0...
Workshop Alex
+2  A: 

You can check the local-name of the element and ignore namespace with the following XPath expression:

//*[local-name()='Provenance1']
Mads Hansen