473,573 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Headache with namespaces ...

Hi

I am having trouble with namespaces:

Using the .NET System.XML classes, whenever I run an xpath on a file with a
namespace, the xpath query returns nothing, but when I remove the namespace
from the xml file I am quering, the xpath query works.

I am trying to run a SelectSingleNod e(xpath) function on an XmlNode, but
when the source xml file has a default namespace, for example:
"xmlns="urn:bla h-org:v3" in the root node, then the SelectSingleNod e function
does not return anything.

When I remove the default namespace from the source xml file, the the
SelectSingleNod e(xpath) function/query works.

What should the xpath be to be able to select nodes from a xml file with a
namespace? Or am I missing something else.

Thanks for you time and your help!

Carlo
Nov 12 '05 #1
2 9137


Carlo Garcia wrote:

Using the .NET System.XML classes, whenever I run an xpath on a file with a
namespace, the xpath query returns nothing, but when I remove the namespace
from the xml file I am quering, the xpath query works.

I am trying to run a SelectSingleNod e(xpath) function on an XmlNode, but
when the source xml file has a default namespace, for example:
"xmlns="urn:bla h-org:v3" in the root node, then the SelectSingleNod e function
does not return anything.

When I remove the default namespace from the source xml file, the the
SelectSingleNod e(xpath) function/query works.

What should the xpath be to be able to select nodes from a xml file with a
namespace? Or am I missing something else.


XPath 1.0 doesn't know a default namespace so you need to assign a
prefix to that namespace and use the prefix in your XPath expression e.g.
/prefix:element-name
You do not have to change anything in the XML file but your .NET code
has to make use of a NamespaceManage r e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"whatever.xm l");

XmlNamespaceMan gager namespaceManage r = new
XmlNamespaceMan ager(xmlDocumen t.NameTable)

namespaceManage r.AddNamespace( "prefix", "urn:blah-org:v3");
then you can use
xmlDocument.Sel ectSingleNode("/prefix:element-name", namespaceManage r)

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Excellent, thanks Marting ... that did the trick!

"Martin Honnen" wrote:


Carlo Garcia wrote:

Using the .NET System.XML classes, whenever I run an xpath on a file with a
namespace, the xpath query returns nothing, but when I remove the namespace
from the xml file I am quering, the xpath query works.

I am trying to run a SelectSingleNod e(xpath) function on an XmlNode, but
when the source xml file has a default namespace, for example:
"xmlns="urn:bla h-org:v3" in the root node, then the SelectSingleNod e function
does not return anything.

When I remove the default namespace from the source xml file, the the
SelectSingleNod e(xpath) function/query works.

What should the xpath be to be able to select nodes from a xml file with a
namespace? Or am I missing something else.


XPath 1.0 doesn't know a default namespace so you need to assign a
prefix to that namespace and use the prefix in your XPath expression e.g.
/prefix:element-name
You do not have to change anything in the XML file but your .NET code
has to make use of a NamespaceManage r e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"whatever.xm l");

XmlNamespaceMan gager namespaceManage r = new
XmlNamespaceMan ager(xmlDocumen t.NameTable)

namespaceManage r.AddNamespace( "prefix", "urn:blah-org:v3");
then you can use
xmlDocument.Sel ectSingleNode("/prefix:element-name", namespaceManage r)

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3

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

Similar topics

18
3026
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less...
24
3490
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
2
1583
by: Mike Morse | last post by:
What see sample that show xs:element where the xs namespace = http://www.w3.org/2001/XMLSchema However, I see another example with xsi: where xsi = http://www.w3.org/2001/XMLSchema-instance What's the difference here? Are there right and wrong namespaces? Is there a global list of the ones to use? Does the namespace even need to point...
3
2314
by: Jim Heavey | last post by:
Trying to get the hang of Namespaces. I have primarly developed in VB and am transitioning to C# and the .Net Environment. I have worked a bit with Java as well in school about a year or so ago. NameSpaces and Java Packages are similar in nature? In the dot net world, you are not able to pick out (import) a particulare "class" within the...
17
2061
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not write a signature for the developer and after getting a good start developing a 2.0 application I thought I should go through the code and start marking...
11
1747
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I understand that you declare your intention to use a namespace within a page through the "Inherits" attribute. I know that using "Inherits" isn't...
0
7755
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...
0
7679
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...
0
7992
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. ...
0
8190
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...
1
5571
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...
0
3722
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...
1
2183
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 we have to send another system
1
1284
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1027
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...

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.