views:

396

answers:

0

Hi all.

I'm trying to display (using a asp:Repeater and a asp:XmlDataSource) the content of a sitemap.xml file (see standard defined into http://www.sitemaps.org/protocol.php).

The problem is that the binder <%#XPath("myNodeName")%> can't work when the source xml document contains custom namespaces (like required by the standard that describes the sitemap.xml format)

Scott Hanselman suggests to use a custom XmlNamespaceManager but I'm not able to initialize it in the correct way, so the <%#XPath("myNodeName")%> doesn't display the values into the XML.

Note that removing the namespaces from the xml, the following sample correctly works !

Source XML file:

<?xml version="1.0" encoding="utf-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"&gt;
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ContentItems/Public/ContentItems_View.aspx?RCICode=rci_001&lt;/loc&gt;
  <changefreq>weekly</changefreq>
  <priority>1.00</priority>
 </url>
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ProductCustomer/Public/ProductCustomer_Descriptor.aspx&lt;/loc&gt;

  <changefreq>weekly</changefreq>
  <priority>0.90</priority>
 </url>
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ContentItems/Public/ContentItems_View.aspx?RCICode=rci_633765577264687500&lt;/loc&gt;
  <changefreq>weekly</changefreq>
  <priority>0.80</priority>

 </url>
</urlset>

ASPX (note the NsMan used inside the the XPath binder):

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
    <ItemTemplate>
        <div>
            Loc: <asp:TextBox ID="LocTB" runat="server" Text='<%#XPath("loc",NsMan)%>' />
            Change frequency: <asp:TextBox ID="ChangeFrequencyTB" runat="server" Text='<%#XPath("changefreq",NsMan)%>' />
            Priority: <asp:TextBox ID="PriorityTB" runat="server" Text='<%#XPath("priority",NsMan)%>' />
        </div>
    </ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/SiteMap.xml" ></asp:XmlDataSource>

Code behind that initialize the NsMan property, used by the XPath binder inside the ASPX.

Probably the error is here !!!

 public XmlNamespaceManager NsMan
    {
        get 
        {
            if (_lazyNsMan == null)
            {
                NameTable table = new NameTable();               
                _lazyNsMan = new XmlNamespaceManager(table);
                _lazyNsMan.AddNamespace("", "http://www.sitemaps.org/schemas/sitemap/0.9");
                _lazyNsMan.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");                
                _lazyNsMan.AddNamespace("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");                
            }

            return _lazyNsMan;
        }
    }