473,513 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Namespaces & .NET XPaths

Just out of interest - is there an easier/quicker way to retrieve values via
Xpath when namespaces are used?

At the moment I seem to need a lot more lines of code to do something I
thought was fairly standard? Of course I can just wrap the calls up into a
helper function but was wondering if there was an easier way...

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("xs", http://mynamespace.com);

node = doc.SelectSingleNode("//xs:person",nsmgr);

... pluck from node...

OR

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("xs", http://mynamespace.com);

xpExp = nav.Compile("string(//xs:person/@name)");

xpExp.SetContext(xmlNSM);

string name = nav.Evaluate(xpExp).ToString(); (BTW: is ToString()
any different to casting (string) )


Nov 11 '05 #1
3 1824
Steve Robinson wrote:
Just out of interest - is there an easier/quicker way to retrieve values via
Xpath when namespaces are used?


If you mean using NamespaceManager - no, you cannot avoid using it when
seleting namespaced nodes, unless your XPath expression is
namespace-prefix-neutral, like
//*[local-name='person' and namespace-uri()='http://mynamespace.com']
instead of
//xs:person

And in fact I don't see any problem with using NamespaceManager - anyway
must be some way to say what xs namespace prefix you are using points
to, becuase arbitrary XML doc may declare xs namespace prefix twice,
each time binding it to a different namespace URI's:
<xs:foo xmlns:xs="foo">
<xs:bar xmlns:xs="bar">blah</xs:bar>
</xs:foo>
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
I guess I just hate having to compile an expression when I do an Evaluate
rather than Select.

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("xs", http://mynamespace.com);
xpExp = nav.Compile("string(//xs:person/@name)");
xpExp.SetContext(xmlNSM);
string name = (string) nav.Evaluate(xpExp);

if I need to pull another value out I need to compile a new expression,
setcontext and Evaluate again...and so on...

xpExp = nav.Compile("string(//xs:person/@department)");
xpExp.SetContext(xmlNSM);
string department = (string) nav.Evaluate(xpExp);

I could always wrap it all up in helper functions or a class, but Ideally it
would have been good if I could at least reuse the same expression object
and just the path or SetContext() would remain valid -

nav.SetContext(namespace); // set namespace for xpath
val1 = nav.Evaluate();
val2 = nav.Evaluate();


"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Steve Robinson wrote:
Just out of interest - is there an easier/quicker way to retrieve values via Xpath when namespaces are used?


If you mean using NamespaceManager - no, you cannot avoid using it when
seleting namespaced nodes, unless your XPath expression is
namespace-prefix-neutral, like
//*[local-name='person' and namespace-uri()='http://mynamespace.com']
instead of
//xs:person

And in fact I don't see any problem with using NamespaceManager - anyway
must be some way to say what xs namespace prefix you are using points
to, becuase arbitrary XML doc may declare xs namespace prefix twice,
each time binding it to a different namespace URI's:
<xs:foo xmlns:xs="foo">
<xs:bar xmlns:xs="bar">blah</xs:bar>
</xs:foo>
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #3
Steve Robinson wrote:
I guess I just hate having to compile an expression when I do an Evaluate
rather than Select.


Well, I tend to agree with you, but... Let's wait PDC and see what they have
prepared in V2 API for us.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4

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

Similar topics

0
1804
by: bdinmstig | last post by:
I am building various framework components for my team to use in development, and one of those components is a Facade for reading/writing user preferences. The idea is that preference settings...
18
2050
by: Hrvoje Somun | last post by:
<example xmlns:namespaceone="http://www.one.com" xmlns="http://www.one.com"> <check att1="one" att2="two" /> <check att1="one" namespaceone:att1="two" /> </example> in my opinion line <check...
0
1410
by: Daniel Yacob | last post by:
Greetings, I'm looking for a Java API that would read an XML doc, as a DOM would be fine, and spit out a list of xpaths and corresponding values for all leaf nodes. Is there such a thing? If...
6
2211
by: Stefan Franke | last post by:
Hi, I've got a little bit of a problem when dealing with namespaces and XPath. I'm trying very basic things, like showing all the nodes of one particular namespace. Here is my XPath statement:...
19
16075
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"...
0
934
by: Kathy Burke | last post by:
Hi, within asp.net, I'm using xpaths to retrieve data from xml strings. For example: Dim n10 As XmlElement = xDoc.SelectSingleNode("//Station/Timer") The Timer element has several attributes...
2
1284
by: daz_oldham | last post by:
Hi All I am re-visiting XSLT after not touching it for about three years, so to be honest I am starting myself from scratch. If I have XML under the namespace "http://tempuri.com/abc/2002/09",...
7
1832
by: Bilal | last post by:
Hello all, I came across this problem while working out the bugs in my identity trasnformation stylesheets but sidestepped it for later to see if there is an easier/better solution. This is...
2
1607
by: lanegroups | last post by:
Dear experts, I'm trying to do the following using Xalan-C++ API 1. parse an XML document 2. evaluate an XML path 3. serialize the selected XML subtree 4. The new serialized XML fragment should...
0
7166
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
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5689
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,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.