Groups | Blog | Home
all groups > dotnet xml > january 2007 >

dotnet xml : XPath query with node in empty namespace xmlns=""


olympus_mons NO[at]SPAM gmx.de
1/15/2007 3:58:33 PM
Hi all,

my XML looks like:

<root xmlns="tempuri.org">
<result xmlns="">
<date>2006-09-14</date>
<status>ok</status>
</result>
</root>

How do I query the status node using .NET 2.0 and XPath?

The only way I found is using local-name():

strState =
xmlDoc.SelectSingleNode("//*[local-name()='status']").InnerText;

Is there another way?

Thanks for your attention and time,
Stefan
Bjoern Hoehrmann
1/16/2007 1:29:24 AM
* olympus_mons@gmx.de wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

You can setup an XmlNamespaceManager as explained in the MSDN docs, and
generally it would be better to use "/root/result/status" instead of the
more complex expression you are using.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
olympus_mons NO[at]SPAM gmx.de
1/16/2007 2:03:49 AM
Bjoern,

thanks for your feedback. I got it. It's quite easy with .NET. I first
tested my XPath in XMLSpy and its XPath evaluator seems to have
problems with default and empty NSs. But now I just tried with .NET,
defined a NS "ns" for "tempuri.org" using the NS manager and then the
XPath just is:

/ns:root/result/status

Stefan


Bjoern Hoehrmann schrieb:

[quoted text, click to view]
e/
Martin Honnen
1/16/2007 4:42:24 PM
[quoted text, click to view]

One way would be the XPath expression /*/result/status e.g.
xmlDoc.SelectSingleNode("/*/result/status")
or
xmlDoc.DocumentElement.SelectSingleNode("result/status)



--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button