Namespaces and the XmlDataSource Server Control

Scott Hanselman discussed this earlier, but the XmlDataSource control has a bug in it. When working with the XPath capablities of the control (which you will do pretty much every time you use the control), the control will throw an exception when working with XML data that includes namespaces/prefixes. I recently encountered this problem when I was working with some of the XML data coming from the Amazon APIs.

One possible fix is to apply a XSLT stylesheet to strip the namespaces and prefixes from the XML. The XSL doc you would use would look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   <xsl:template match="*">
      <!-- Remove any prefixes -->
      <xsl:element name="{local-name()}">
          <!-- Work through attributes -->
          <xsl:for-each select="@*">
             <!-- Remove any attribute prefixes -->
             <xsl:attribute name="{local-name()}">
                <xsl:value-of select="."/>

Then with the XSL doc in place, you can then use the XmlDataSource control to make use of this document:

<asp:XmlDataSource ID="XmlDataSource1" runat="server"
 DataFile="NamespaceFilled.xml" TransformFile="~/RemoveNamespace.xsl"