Karine Bosch wrote:
My xml document uses different namespaces:
<x:root xmlns:x="x:ns:meta/">
<element1>a</element1>
<element1>b</element1>
...
<element1 xmlns:adam="http://ns.alfaprint.be/">x</element1>
<element1>y</element1>
<element1>z</element1>
...
</root>
I see two namespace declared but only one being used as only the x:root
element is in a namespace, the namespace with URI x:ns:meta/. I don't
see any element or attribute in the namespace with URI
http://ns.alfaprint.be/, there is only a declaration binding the prefix
adam to that namespace URI.
I tried the following 2 things:
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:root[@xmlns:adam='http://ns.alfaprint.be/']",
XmlNode oXmlNode =
oXmlRoot.SelectSingleNode("//x:root[@xmlns:adam='http://ns.alfaprint.be/']",
mgr2);
- As the document can contain hundreds of elements, it's clear that I'm
looking to extract that one node that is of interest for me by using a
SelectSingleNode.
But which node exactly are you looking for? I have already told you if
you want the root element in your example then
xmlDocument.SelectSingleNode("/x:root", mgr1)
will give you the root element.
It is not clear what
//x:root[@xmlns:adam='http://ns.alfaprint.be/']
is supposed to find in your example document.
//x:root
makes some sense in general but in your example markup
/x:root
would suffice. But use
//x:root
if you want/need.
What the predicate
@xmlns:adam='http://ns.alfaprint.be/'
however is supposed to do I don't know, as said namespace declarations
do not show up as attribute nodes in the XPath data model so writing
@xmlns:adam where @something in XPath alway selects an attribute does
not make sense.
Do an XPath evaluation of e.g.
//@*
or
//*/@*
on that example XML, it will not find any nodes, those namespace
declarations are not attributes in terms of XPath.
If you want to find namespace nodes then you need to use the namespace
axis.
So doing e.g.
//element1[namespace::adam]
would find you the element with the markup
<element1 xmlns:adam="http://ns.alfaprint.be/">x</element1>
in your example markup. No namespace manager is needed in .NET to
evaluate that expression.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/