If you have ever worked with XML, you know that XPath is your friend.
Today I working with an XML document that was generated via MSWord and
the generated document was well formed and included namespaces. Now, I have worked with namespaces in XML in
the past, but it has been a LONG time. I
thought I would put up a post on how to perform simple queries in a XML
document that has namespaces because it does involve a little more work then
when there is no namespaces.
Sample XML Document
Sample Code to query the XML Document
Code explanation
- Getting the namespace information
In order to use the namespace URI information you will need to pull this out of
the xmlDocument object. You can do this by calling the method GetNamespaceOfPrefix and provide it the name of the namespace. In this case that is found in workSheetXPath.
- Creating the XmlNamespaceManager
Once you have the namespace, you need to build the namespace manager
object (this is used later during the actual query). This is pretty straight forward, but is
required.
- Building the XPath
query
If you have ever built a XPath
query before, this should look familiar, but with a twist. When there is namespace information in the
document, this has to be prepended to EVERYTHING in
the XPath query.
Notice I am using the string.Format to allow for cleaner code.
The final output of this query is "//ss:Worksheet[@ss:Name=’Sheet1']/ss:Table/ss:Row"
- Calling/Using the XPath
query
Executing the XPath is not
much different, but you will need to provide the namespaceManager
object created above in order to get this to work. If you do not, you will get a run time
exception.
There you go, a simple how-to on querying
an XML document that has namespace information in it.
Till next time,
Posted
08-03-2007 12:28 PM
by
Derik Whittaker