Groups | Blog | Home
all groups > dotnet xml > june 2003 >

dotnet xml : XmlNamespaceManager, default namespaces, nested qualified namespace, xpath problem.


Oisin Grehan
6/30/2003 11:35:18 AM
Hi,


I can't for the life of me get this to work properly. I've searched for
examples, but none of the examples quite match my environment. Here is my
XML (please treat this as immutable -- no solutions involving altering the
XML are good for me):

---- config.xml ----

<Configuration
xmlns="urn:server:schemas:configuration"
xmlns:f="urn:server:schemas:faq">
<version version="0.0.0001">
<f:faq>
<f:section>Default</f:section>
<f:item>
<f:q>What is the meaning of life?</f:q>
<f:a>42.</f:a>
</f:item>
</f:faq>
</version>
</Configuration>

I want to have an XmlNode object referencing the first <faq> tag. Here is
the corresponding c# code:

XmlDocument xd = new XmlDocument();
xd.Load("config.xml");
XmlNamespaceManager xnm = new XmlNamespaceManager(xd.NameTable);
xnm.AddNamespace(String.Empty, "urn:server:schemas:configuration");
xnm.AddNamespace("f", "urn:server:schemas:faq"); // faq container namespace
XmlNode xnFaq = xd.SelectSingleNode(

"/Configuration/version[@version=\"0.0.0001\"]/f:faq[f:section=\"Default\"]"
, xnm);

This fails to select the f:faq node. I've tried using the following also:

xnm.AddNamespace("c", "urn:server:schemas:configuration");
xnm.AddNamespace("f", "urn:server:schemas:faq"); // faq container namespace
XmlNode xnFaq = xd.SelectSingleNode(

"/c:Configuration/c:version[@c:version=\"0.0.0001\"]/f:faq[f:section=\"Defau
lt\"]", xnm);

I'm beginning to lose the mind here people; any ideas? Cheers!

- Oisin




Oisin Grehan
6/30/2003 12:44:43 PM
Right -- for the benefit of the rest of the group, the problem was in the
XPath query;

if should have been:

/c:Configuration/c:version[@version=\"0.0.0001\"] ...

instead of

/c:Configuration/c:version[@c:version=\"0.0.0001\"] ...

I was qualifying the attribute with a namespace prefix and I didn't need to.
Guess I'd better read up a little more on schema defaults :)

- Oisin



[quoted text, click to view]

<snip />

AddThis Social Bookmark Button