Home All Groups Group Topic Archive Search About

Headache with namespaces ...

Author
15 Nov 2004 10:05 AM
Carlo Garcia
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

Author
15 Nov 2004 12:58 PM
Martin Honnen
Carlo Garcia wrote:


Show quote
> 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/
Author
18 Nov 2004 9:53 AM
Carlo Garcia
Excellent, thanks Marting ... that did the trick!

Show quote
"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/
>

AddThis Social Bookmark Button

Post Other interesting topics