469,323 Members | 1,667 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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 8969


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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.