Groups | Blog | Home
all groups > dotnet xml > august 2004 >

dotnet xml : XPath Query w/ Namespace



Scot NS Curry
8/26/2004 3:45:30 PM
I have the following XML document.

<?xml version="1.0" encoding="utf-16"?>
<xd:xmldiff version="1.0" srcDocHash="6067335156532207495" options="None"
fragments="no"
xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
<xd:node match="1">
<xd:node match="1">
<xd:node match="4">
<xd:node match="3">
<xd:change match="@version">2</xd:change>
</xd:node>
</xd:node>
</xd:node>
</xd:node>
</xd:xmldiff>

If I take the prefixes off, the following query works as I would expect:
XmlNodeList rootNodes = changeDoc.SelectNodes("/xmldiff/node", nsmanager);

What does the code look like with the namespace prefix?

TIA.
Scot

Derek Harmon
8/26/2004 6:43:02 PM
[quoted text, click to view]

Did you add the XmlDiff namespace URI to nsmanager? and
if so, what prefix did you give it? That prefix must appear in
the XPath expression, too.

XmlNamespaceManager nsManager =
new XmlNamespaceManager( changeDoc.NameTable);
nsManager.AddNamespace( "diff",
"http://schemas.microsoft.com/xmltools/2002/xmldiff");
XmlNodeList rootNodes = changeDoc.SelectNodes(
"/diff:xmldiff/diff:node", nsManager);

I use 'diff' to demonstrate that the choice of namespace prefix
added to the XmlNamespaceManager is irrelevant, it's the
namespace URI that counts.


Derek Harmon

AddThis Social Bookmark Button