ASP.NET 2.0 XmlDataSource 的 XPath 不支持命名空间

发布于 2024-08-10 07:41:04 字数 3189 浏览 4 评论 0原文

我正在尝试使用 asp:Repeaterasp:XmlDataSource 显示 sitemap.xml 文件的内容(请参阅定义为http://www.sitemaps.org/protocol.php)。

问题是,当源 XML 文档包含自定义命名空间(如描述 的标准所要求的那样)时,绑定器 <%#XPath("myNodeName")%> 无法工作sitemap.xml 格式)。

Scott Hanselman 建议使用自定义 XmlNamespaceManager,但我无法以正确的方式初始化它,因此 <%#XPath("myNodeName")%> 不将值显示到 XML 中。

请注意,通过从 XML 中删除命名空间,以下示例可以正常工作!

源 XML 文件:

<?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">
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ContentItems/Public/ContentItems_View.aspx?RCICode=rci_001</loc>
  <changefreq>weekly</changefreq>
  <priority>1.00</priority>
 </url>
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ProductCustomer/Public/ProductCustomer_Descriptor.aspx</loc>

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

 </url>
</urlset>

ASPX(注意 XPath 绑定器内使用的 NsMan):

<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>

初始化 NsMan 属性的代码,由 ASPX 内的 XPath 绑定器使用(这是最有可能出现错误的地方):

 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;
        }
    }

I'm trying to display the content of a sitemap.xml file using an asp:Repeater and an asp:XmlDataSource (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 by 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">
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ContentItems/Public/ContentItems_View.aspx?RCICode=rci_001</loc>
  <changefreq>weekly</changefreq>
  <priority>1.00</priority>
 </url>
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ProductCustomer/Public/ProductCustomer_Descriptor.aspx</loc>

  <changefreq>weekly</changefreq>
  <priority>0.90</priority>
 </url>
 <url>
  <loc>http://www.firebenchmarks.com/Modules/ContentItems/Public/ContentItems_View.aspx?RCICode=rci_633765577264687500</loc>
  <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 (This is where the error most likely is):

 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;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文