473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

SelectSingleNode with multiple namespaces (sort of)...

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.

Nov 12 '05 #1
3 5317
455 wrote:
_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/de...ml05202002.asp

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
455
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
Nov 12 '05 #3
455 wrote:
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()='']

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adam | last post by:
I tried so many ways to select the node but its not working, please help. I want to research by the identifier in imsmanifest.xml file, the node could be item or resource. XmlDocument doc = new...
1
by: RedEagle | last post by:
Hi all! I have been dealing with a really strange problem with this xml file: <?xml version="1.0" encoding="UTF-8" ?> <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">...
6
by: David Thielen | last post by:
Hi; I am calling SelectSingleNode("/xml/s:Schema/s:ElementType/@name") where "/xml/s:Schema/s:ElementType/@name is a legit xpath statement (xml is the name of the rootnode) and that xpath...
3
by: Jonathan | last post by:
Hi, I use a XML-Doc with a Namespace like the following example: <HomedResources xmlns=http://schemas.microsoft.com/RtcServer/2002/11/dbimpexp Version="3"> <HomedResource...
1
by: Angela | last post by:
I am getting an error when I attempt to access a node with SelectSingleNode(): "The expression passed to this method should result in a NodeSet." I understand there is some confusion when you have...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
3
by: 455 | last post by:
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"...
1
by: icfai | last post by:
hi friends.... I have got a problem regarding loading of multiple assemblies, actually its required for an editor which implements the intellisenseas in vb or dotnet. for that it is required to...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.