Groups | Blog | Home
all groups > dotnet xml > november 2004 >

dotnet xml : SelectSingleNode with multiple namespaces (sort of)...


455
11/30/2004 6:21:43 PM
Hello all,

I've been trying to figure this out for hours now... can anyone help?

I have an XML document like this:

<?xml version="1.0" encoding="utf-8" ?>
<ICE:ServiceCall xmlns="ICE" xmlns:ICE="http://www.ice.net">
<ICE:Service name="">
<ICE:RetVal/>
<ICE:ConnString/>
<ICE:Params>
<Subject>TEST</Subject>
</ICE:Params>
</ICE:Service>
</ICE:ServiceCall>

For the life of me, I cannot selectsinglenode() to the Subject.

Here's the code:

XmlNamespaceManager _xnsmgr = new XmlNamespaceManager(xRequest.NameTable);

_xnsmgr.AddNamespace(string.Empty,"ICE");

_xnsmgr.AddNamespace("ICE",http://www.ice.net);

try

{

//THIS IS ALWAYS FAILING!!!

sSubject = xRequest.SelectSingleNode("//ICE:Params/Subject",
_xnsmgr).InnerText;

}

catch (Exception ex)

{

string sDEBUG = ex.Message;

}



Any help would be extremely .. um... helpful.

Thanks.




455
11/30/2004 7:08:10 PM
Oleg, thanks for your response. I've got it working (thanks to you),
however, I am worried about the complexity for other developers when they
implement my solution (and need to get the ICE:Params child nodes).

Is this the only option? Is there any way I can modify the XML document so
that I can do this:

sSubject = xRequest.SelectSingleNode("//Subject").InnerText

.... where I maintain the ICE: prefix on the parent nodes?

Thanks,

455

Oleg Tkachenko [MVP]
11/30/2004 8:54:43 PM
[quoted text, click to view]

This is useless. XPath 1.0 doesn't support default namespace. You have
to use some dummy prefix to register "ICE" namespace. (Note, you don't
have to modify source XML, because prefix doesn't matter).

_xnsmgr.AddNamespace("foo","ICE");

[quoted text, click to view]

sSubject = xRequest.SelectSingleNode("//ICE:Params/foo:Subject",
_xnsmgr).InnerText;

Read "XML Namespaces and How They Affect XPath and XSLT"
at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml05202002.asp

--
Oleg Tkachenko [XML MVP]
Oleg Tkachenko [MVP]
11/30/2004 9:21:42 PM
[quoted text, click to view]

Remove the default namespace declaration then (xmlns=""). In XPath 1.0
non-prefixed name like Subject means element named Subject in no
namespace. The rule of thumb - if you want to select an element in a
namespace, you have to use prefixed name in XPath.
An alternative way is

//*[local-name()='Subject' and namespace-uri()='']

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button