SelectSingleNode with multiple namespaces (sort of)...

  • Thread starter Thread starter 455
  • Start date Start date
4

455

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 said:
_xnsmgr.AddNamespace(string.Empty,"ICE");

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");
_xnsmgr.AddNamespace("ICE",http://www.ice.net);
sSubject = xRequest.SelectSingleNode("//ICE:Params/Subject",
_xnsmgr).InnerText;

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, 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
 
455 said:
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

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()='']
 
Back
Top