473,796 Members | 2,509 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...

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(doc.NameTa ble);

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

node = doc.SelectSingl eNode("//xs:person",nsmg r);

... pluck from node...

OR

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(doc.NameTa ble);

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

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

xpExp.SetContex t(xmlNSM);

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


Nov 11 '05 #1
3 1840
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 NamespaceManage r - 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 NamespaceManage r - 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.

XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(doc.NameTa ble);
nsmgr.AddNamesp ace("xs", http://mynamespace.com);
xpExp = nav.Compile("st ring(//xs:person/@name)");
xpExp.SetContex t(xmlNSM);
string name = (string) nav.Evaluate(xp Exp);

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("st ring(//xs:person/@department)");
xpExp.SetContex t(xmlNSM);
string department = (string) nav.Evaluate(xp Exp);

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_P LEASEtkachenko. com> wrote in message
news:%2******** *******@TK2MSFT NGP11.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 NamespaceManage r - 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 NamespaceManage r - 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
1824
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 are stored in a free-format XML document (in memory for the life of the session) and persisted to a database (as free text) on exit. I have taught my developers the basics of XPath, however I don't want to have to review XMLDOM code all over the...
18
2078
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 att1="one" namespaceone:att1="two" /> is wrong (different namespaces for aributes of one element are not allowed)
0
1421
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 not, I can imagine accomplishing this with a tree walk over a DOM object and stuffing the path/value info into a hash along the way. Is there a better way to go about it?
6
2229
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: //* Unfortunately this doesn't work. I have found several ways to solve this with XSLT, but I need the pure XPath statement.
19
16225
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" xmlns:sns="http://www.test.org/sub" xmlns:mns="http://www.test.org/mini"> <data>
0
949
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 (on, off, etc.) that I would like to retrieve and set as a variable. I've been using xmlAttribute = xDoc.SelectSingleNode("//Station/Timer/@on") etc. to get the
2
1297
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", should I declare this at the top of my XSLT? Additionally, if I have the following XML:
7
1845
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 essentially following up to my last post so apologies for repeating parts of the post. The transformation stylesheet is (after corrections from kind contributors :) <?xml version="1.0" encoding="UTF-8"?>
2
1620
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 have the correct namespace declared, based on their declaration from the original XML document. Task 1-3 are done and I'm using the traverseSubtree method from FormatterTreeWalker class to serialize the subtree. However, the
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7548
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.