Ralph Shillington
Ralph Shillington

Reputation: 21108

SelectSingleNode without namespace

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.

Upvotes: 20

Views: 15217

Answers (2)

Wim ten Brink
Wim ten Brink

Reputation: 26682

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

Upvotes: 0

Mads Hansen
Mads Hansen

Reputation: 66781

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

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

Upvotes: 41

Related Questions