After looking at how to use XPath to select nodes I enhanced what I found at this page:
http://msdn.microsoft.com/en-us/library/3tk3f03k.aspx
and added an attribute cat="psychology" that Im trying to query like this: "//ab:book/ab:title/@ab:cat". Contrary to what I expected I wont get any results until I remove the prefix from the attribute, like this: "//ab:book/ab:title/@cat".
Im really annoyed with that behavior as Im processing XML files that have attributes in several namespaces. How should I proceed, is there something Im missing?
Eric.
PS. Heres my sample code:
<pre lang="x-c# [TestMethod]
public void TestXpathSelect4()
{
string xml =
@"<bookstore xmlns=""http://www.lucernepublishing.com"" >
<book>
<title cat=""psychology" Pride And Prejudice</title>
</book>
</bookstore>";
XPathDocument document = new XPathDocument(new StringReader(xml));
var nav = document.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XPathExpression expr = nav.Compile("//ab:book/ab:title/@ab:cat");
expr.SetContext(nsmgr);
string value = null;
XPathNodeIterator ni = nav.Select(expr);
while (ni.MoveNext())
{
value = ni.Current.Value;
}
Assert.AreEqual("psychology", value);
}[/code]
<br/>
View the full article
http://msdn.microsoft.com/en-us/library/3tk3f03k.aspx
and added an attribute cat="psychology" that Im trying to query like this: "//ab:book/ab:title/@ab:cat". Contrary to what I expected I wont get any results until I remove the prefix from the attribute, like this: "//ab:book/ab:title/@cat".
Im really annoyed with that behavior as Im processing XML files that have attributes in several namespaces. How should I proceed, is there something Im missing?
Eric.
PS. Heres my sample code:
<pre lang="x-c# [TestMethod]
public void TestXpathSelect4()
{
string xml =
@"<bookstore xmlns=""http://www.lucernepublishing.com"" >
<book>
<title cat=""psychology" Pride And Prejudice</title>
</book>
</bookstore>";
XPathDocument document = new XPathDocument(new StringReader(xml));
var nav = document.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XPathExpression expr = nav.Compile("//ab:book/ab:title/@ab:cat");
expr.SetContext(nsmgr);
string value = null;
XPathNodeIterator ni = nav.Select(expr);
while (ni.MoveNext())
{
value = ni.Current.Value;
}
Assert.AreEqual("psychology", value);
}[/code]
<br/>
View the full article