20

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.

Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154

2 Answers2

40

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

//*[local-name()='Provenance1']
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
0

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.)

Wim ten Brink
  • 25,901
  • 20
  • 83
  • 149
  • This throws an XPathException //*:Provenance1 has an invalid token. – Ralph Shillington Sep 29 '09 at 13:25
  • 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... – Wim ten Brink Sep 29 '09 at 20:01