<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>John Pollard's Technical Blog : XML</title><link>http://blogs.msdn.com/john_pollard/archive/tags/XML/default.aspx</link><description>Tags: XML</description><dc:language>en-GB</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Using SelectSingleNode (or SelectNodes) on XML where the default namespace has been set</title><link>http://blogs.msdn.com/john_pollard/archive/2005/11/12/using-selectsinglenode-or-selectnodes-on-xml-where-the-default-namespace-has-been-set.aspx</link><pubDate>Sat, 12 Nov 2005 17:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:492093</guid><dc:creator>John Pollard</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/john_pollard/comments/492093.aspx</comments><wfw:commentRss>http://blogs.msdn.com/john_pollard/commentrss.aspx?PostID=492093</wfw:commentRss><wfw:comment>http://blogs.msdn.com/john_pollard/rsscomments.aspx?PostID=492093</wfw:comment><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I've been stumped by this one at least two times over the last couple of years, so I thought it was a good candidate to be written up here.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I was trying to select a node from some standard XHTML where the default namespace was set. In otherwords the XHTML was something like:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "&lt;/FONT&gt;&lt;A href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;FONT face="Courier New"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New"&gt;"[]&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;html xmlns="&lt;/FONT&gt;&lt;A href="http://www.w3.org/1999/xhtml"&gt;&lt;FONT face="Courier New"&gt;http://www.w3.org/1999/xhtml&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New"&gt;" lang="en" xml:lang="en"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;head&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;title&amp;gt;MSN Search News: Microsoft&amp;lt;/title&amp;gt; ...&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Note the &lt;FONT face="Courier New"&gt;xmlns&lt;/FONT&gt; attribute on the root &lt;FONT face="Courier New"&gt;&amp;lt;html&amp;gt;&lt;/FONT&gt; node.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Without thinking too hard, I first tried to find the title of the page by going ...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlDocument resultsXhtml = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; XmlDocument();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;resultsXhtml.Load("&lt;/FONT&gt;&lt;A href="http://search.msn.com/news/results.aspx?q=Microsoft"&gt;&lt;FONT face="Courier New"&gt;http://search.msn.com/news/results.aspx?q=Microsoft&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="Courier New"&gt;");&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlNode metaNode = resultsXhtml.SelectSingleNode("//title");&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana&gt;... which left metaNode as null.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana&gt;This took me a little while to figure out. Clearly I need to identify in the XPath query that the title tag is in the default namespace, but how can I do that if that namespace has no prefix in the actual XML.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana&gt;The solution (reasonably obviously!) is to register a prefix of my own choosing in an XmlNamespaceManager object, and then use that namespace manager when doing the select. Here's some code that works:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlDocument resultsXhtml = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; XmlDocument();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;resultsXhtml.Load("&lt;A href="http://search.msn.com/news/results.aspx?q=Microsoft"&gt;http://search.msn.com/news/results.aspx?q=Microsoft&lt;/A&gt;");&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlNamespaceManager namespaceManager = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; XmlNamespaceManager(resultsXhtml.NameTable);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;namespaceManager.AddNamespace("myprefix", "http://www.w3.org/1999/xhtml");&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlNode metaNode = resultsXhtml.SelectSingleNode("//myprefix:title", namespaceManager);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;I think what's interesting about this problem, is the way you have to think about namespaces and XPath queries. The namespace is a logical entity denoted by the URI not the prefix in the actual XML. Therefore you can register that URI with any prefix you want in your XPath, which isn't a completely intuitive concept - to me at least!&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=492093" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/john_pollard/archive/tags/XML/default.aspx">XML</category><category domain="http://blogs.msdn.com/john_pollard/archive/tags/.Net/default.aspx">.Net</category><category domain="http://blogs.msdn.com/john_pollard/archive/tags/Tips/default.aspx">Tips</category></item></channel></rss>