xpath and namespace issue

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,

I am working with xml and xpath to parse values from xml document. All work fine untill we have namespace used in xml document.
Here is sample xml with namespace in it:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; <span style="color:Blue; 1.0<span style="color:Black; <span style="color:Blue; ?>
<span style="color:Green; <!-- This file represents a fragment of a book store inventory database -->
<span style="color:Blue; <<span style="color:#A31515; bookstore <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.rixml.org/2005/3/RIXML<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; genre<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; autobiography<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title<span style="color:Blue; >The Autobiography of Benjamin Franklin<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; first-name<span style="color:Blue; >Benjamin<span style="color:Blue; </<span style="color:#A31515; first-name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; last-name<span style="color:Blue; >Franklin<span style="color:Blue; </<span style="color:#A31515; last-name<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >8.99<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; genre<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; novel<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title<span style="color:Blue; >The Confidence Man<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; first-name<span style="color:Blue; >Herman<span style="color:Blue; </<span style="color:#A31515; first-name<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; last-name<span style="color:Blue; >Melville<span style="color:Blue; </<span style="color:#A31515; last-name<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >11.99<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; genre<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; philosophy<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title<span style="color:Blue; >The Gorgias<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; name<span style="color:Blue; >Plato<span style="color:Blue; </<span style="color:#A31515; name<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >9.99<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; bookstore<span style="color:Blue; >
[/code]
Here is code that I am using to apply xpath. All works if I manually add namespace to all the elements and then refer the same in all xpath expresions or after removing namespace from the document. Here is code that works fine with namespaces:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; string sRixmlFilePath = <span style="color:#A31515; @"C:Books.xml";
<span style="color:Blue; string sExpression = <span style="color:Blue; string.Empty;

XmlReaderSettings settings = <span style="color:Blue; new XmlReaderSettings();
settings.ValidationType = ValidationType.None;

XmlReader reader = XmlReader.Create(sRixmlFilePath, settings);
XmlDocument document = <span style="color:Blue; new XmlDocument();
document.Load(reader);

<span style="color:Green; // Create a navigator to query with XPath.
XPathNavigator nav = document.CreateNavigator();

<span style="color:Green; //Spec Case 2
sExpression = <span style="color:#A31515; "/bz:bookstore/bz:book/@genre";

XPathExpression expression = nav.Compile(sExpression);

<span style="color:Blue; string namespaceUri = <span style="color:#A31515; "http://www.rixml.org/2005/3/RIXML";
XmlNamespaceManager ns = <span style="color:Blue; new XmlNamespaceManager(<span style="color:Blue; new NameTable());
ns.AddNamespace(<span style="color:#A31515; "bz", namespaceUri);

XPathNodeIterator iterator = nav.Select(sExpression, ns);
<span style="color:Blue; foreach (XPathNavigator item <span style="color:Blue; in iterator)
{
Console.WriteLine(item.Value);
}
[/code]
But I dont want to add this extra namespace and then dont want to refer it in xpath expression. I am sure there would be a proper solution to this problem. Any guidence?
Thanks. <hr class="sig Asim

View the full article
 
Back
Top