473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create 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 SelectSingleNode(xpath) function on an XmlNode, but
when the source xml file has a default namespace, for example:
"xmlns="urn:blah-org:v3" in the root node, then the SelectSingleNode function
does not return anything.

When I remove the default namespace from the source xml file, the the
SelectSingleNode(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 9121


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 SelectSingleNode(xpath) function on an XmlNode, but
when the source xml file has a default namespace, for example:
"xmlns="urn:blah-org:v3" in the root node, then the SelectSingleNode function
does not return anything.

When I remove the default namespace from the source xml file, the the
SelectSingleNode(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 NamespaceManager e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"whatever.xml");

XmlNamespaceMangager namespaceManager = new
XmlNamespaceManager(xmlDocument.NameTable)

namespaceManager.AddNamespace("prefix", "urn:blah-org:v3");
then you can use
xmlDocument.SelectSingleNode("/prefix:element-name", namespaceManager)

--

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 SelectSingleNode(xpath) function on an XmlNode, but
when the source xml file has a default namespace, for example:
"xmlns="urn:blah-org:v3" in the root node, then the SelectSingleNode function
does not return anything.

When I remove the default namespace from the source xml file, the the
SelectSingleNode(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 NamespaceManager e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"whatever.xml");

XmlNamespaceMangager namespaceManager = new
XmlNamespaceManager(xmlDocument.NameTable)

namespaceManager.AddNamespace("prefix", "urn:blah-org:v3");
then you can use
xmlDocument.SelectSingleNode("/prefix:element-name", namespaceManager)

--

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
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...
24
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
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 ...
3
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....
17
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...
11
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...
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
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
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
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...
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,...
1
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
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...
0
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...

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.