472,146 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 9048


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Marcin Vorbrodt | last post: by
2 posts views Thread by Mike Morse | last post: by
3 posts views Thread by Jim Heavey | last post: by
11 posts views Thread by Random | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

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.