xmldatasource_ASP.NET 2.0 XmlDataSource的XPath不支持名称空间

xmldatasource

xmldatasource

I'm working (again) on the XML Chapter to our upcoming book. The book is all about ASP.NET 2.0, but XML is such an important part of ASP.NET that this chapter gets bigger and bigger. I've been updating it from the original Beta 1 version this last few months and noticed that the namespace qualification for the XmlDataSource is still broken/incomplete as it was last year in September. I talked to a bunch of people at TechEd including a number of very helpful devs and PMs who were very much interested in resolving this issue. However, unfortunately it looks like this'll be one of those features that won't make it into the final, which means one of us will have to write our own.

我正在(再次)致力于即将出版的书的XML章节。 本书全都是关于ASP.NET 2.0的,但是XML是ASP.NET的重要组成部分,因此这一章越来越大。 最近几个月,我一直在从原始Beta 1版本进行更新,并注意到XmlDataSource的命名空间资格仍然不如去年9月份/不完整。 我与TechEd的一群人进行了交谈,其中包括一些非常有帮助的开发人员和PM,他们对解决此问题非常感兴趣。 但是,不幸的是,这似乎将成为无法进入决赛的那些功能之一,这意味着我们其中之一将不得不编写自己的功能。

The basic problem is this (from the book draft):

基本问题是这样的(来自书草案):

One unfortunate caveat of the new XmlDataSource is its XPath attribute does not support documents that use namespace qualification. Examples in this chapter use the Books.xml file with a default namespace of http://examples.books.com. It is very common for XML files to use multiple namespaces, including a default namespace. As you learned when you created an XPathDocument and queried it with XPath, the namespace in which an element exists is very important.

新XmlDataSource的一个不幸警告是,其XPath属性不支持使用名称空间限定的文档。 本章中的示例使用带有默认命名空间http://examples.books.com的Books.xml文件 XML文件使用多个名称空间(包括默认名称空间)是很常见的。 正如您在创建XPathDocument并使用XPath查询时所了解的那样,元素所在的名称空间非常重要。

The regrettable reality is, there is no way use a namespace qualified XPath expression or to make the XmlDataSource Control aware of a list of prefix/namespace pairs via the XmlNamespaceManager class. However, the XPath function used in the ItemTemplate of the templated DataList control can take a XmlNamespaceManager as its second parameter and query XML returned from the XmlDataSource - as long as the control does not include an XPath attribute with namespace qualification or you can just omit it all together. That said, in order for these examples to work, you must remove the namespaces from your source XML and use XPath queries that include no namespace qualification, as shown in Listing xx-xx. 

令人遗憾的现实是,无法使用名称空间限定的XPath表达式,也无法通过XmlNamespaceManager类使XmlDataSource控件知道前缀/名称空间对的列表。 但是,在模板化DataList控件的ItemTemplate中使用的XPath函数可以将XmlNamespaceManager作为其第二个参数,并查询从XmlDataSource返回的XML-只要该控件不包含具有名称空间限定的XPath属性,或者您可以忽略它即可。全部一起。 就是说,为了使这些示例起作用,必须从源XML中删除名称空间,并使用不包含名称空间限定符的XPath查询,如清单xx-xx所示。

I was hoping to avoid having any caveats like this in the book, but this one will stay until there's a solution to the problem. It'd be nice if someone (Oleg, kzu, Don, me, you?) could add a namespace-aware XmlDataSource control to the Mvp.Xml project and have it ready by 2.0 ship.

我希望避免在书中有这样的警告,但是这一问题将一直存在,直到解决问题为止。 如果有人( OlegkzuDon ,我,你呢?)可以将一个支持名称空间的XmlDataSource控件添加到Mvp.Xml项目中,并在2.0版本中将其准备好,那就太好了

As it is currently, you do this:

目前,您可以执行以下操作:

<asp:datalist id="DataList1" DataSourceID="XmlDataSource1" runat="server">
    <ItemTemplate>
        <p><b><%# XPath("author/first-name") %> 
              <%# XPath("author/last-name")%></b>
                    wrote <%# XPath("title") %></p>
    </ItemTemplate>
</asp:datalist>
<asp:xmldatasource id="XmlDataSource1" runat="server"
    datafile="~/Books.xml" 
    xpath="//bookstore/book"/>

<asp:datalist id =“ DataList1” DataSourceID =“ XmlDataSource1” runat =“ server”> <ItemTemplate> <p> <b> <%#XPath(“作者/名字”)%> <%#XPath(“作者/姓氏”)%> </ b> 写了<%#XPath(“ title”)%> </ p> </ ItemTemplate> </ asp:datalist> <asp:xmldatasource id =“ XmlDataSource1” runat =“服务器” datafile =“〜/ Books.xml” xpath =“ // bookstore / book ” />

And the root problem is that the boldfaced xpath expression can't use namespace qualified XPath expressions like //b:bookstore/b:book, because there's no way to pass in an XmlNamespaceManager to the XmlDataSource. Note that this doesn't apply to the TemplatedControl.XPath expression. You CAN pass in an XmlNamespaceManager like this: <%# XPath("b:author/b:first-name", myNamespaceMgr) %>. The only bummer is that there's no completely declarative way to do this; you have to have the XmlNamespaceManager in the code behind.

根本问题在于,粗体的xpath表达式不能使用名称空间限定的XPath表达式,例如// b:bookstore / b:book ,因为无法将XmlNamespaceManager传递给XmlDataSource。 请注意,这并不适用于TemplatedControl.XPath表达。 您可以像这样传递XmlNamespaceManager: <%#XPath(“ b:author / b:first-name”,myNamespaceMgr)%> 。 唯一可惜的是,没有完全声明式的方法可以做到这一点。 您必须在后面的代码中具有XmlNamespaceManager。

In an ideal world, there'd be a number of ways to let the XmlDataSource know about namespaces and prefix. Here's some ideas in order of preference:

在理想的世界中,有多种方法可以让XmlDataSource知道名称空间和前缀。 以下是按偏好顺序排列的一些想法:

  • (this wouldn't be hard, methinks)? (方法并不难,方法学)
  • Pass in the prefixes/namespaces declaratively like Christopf wants:  <asp:xmldatasource runat="server" id="xds1"
      datafile="~/app_data/namespacebooks.xml"
      xpath="/ba:store/dx:book">  <asp:namespace prefix="ba" name=”http://bracketangles.net/names” />
      <asp:namespace prefix="dx" name=”http://donxml.com/names” />
      <asp:namespace prefix="ha" name=”http://hanselman.com/names” />
    </asp:xmldatasource>
    Christopf希望的那样以声明方式传递前缀/命名空间: <asp:xmldatasource runat =“服务器” id =“ xds1” datafile =“〜/ app_data / namespacebooks.xml” xpath =“ / ba:store / dx:book”> <asp:namespace prefix =“ ba” name =” http://bracketangles.net/names” /> <asp:namespace prefix =“ dx” name =” http://donxml.com/names” /> <asp:namespace prefix =“ ha” name =“” http://hanselman.com/names” /> </ asp:xmldatasource>
  • Have an event in the code behind like this, where you can help in an associated a NamespaceManager: private void OnXmlDataSource1ExecutingXPath(object sender, XmlDataSourceXPathEventArgs e) {
       NameTable table = new NameTable();
       e.NamespaceManager = new XmlNamespaceManager(table);
       e.NamespaceManager.AddNamespace("a", "b");
       e.NamespaceManager.AddNamespace("myns1", "
    http://www.example.org/namespace1 ");
      e.NamespaceManager.AddNamespace("myns2", "
    http://www.example.org/namespace2 ");
    }
    像这样在后面的代码中有一个事件,您可以在其中协助关联的NamespaceManager: 私人无效OnXmlDataSource1ExecutingXPath(object sender,XmlDataSourceXPathEventArgs e){ NameTable表=新的NameTable(); e.NamespaceManager =新的XmlNamespaceManager(table); e.NamespaceManager.AddNamespace(“ a”,“ b”); e.NamespaceManager.AddNamespace(“ myns1”,“ http://www.example.org/namespace1 ”); e.NamespaceManager.AddNamespace(“ myns2”,“ http://www.example.org/namespace2 ”); }

Any of these would be preferrable to the alternatives, which are, as I seem them:

在我看来,其中任何一种都比其他选择更可取:

  • Not using documents with namespaces

    不使用带名称空间的文档
  • Not using the XPath attribute of the XmlDataSource

    不使用XmlDataSource的XPath属性
  • Using an XSLT Transformation to strip out namespaces before using the result in the XmlDataSource. Yikes.

    在XmlDataSource中使用结果之前,请使用XSLT转换除去名称空间。 kes。

It's a bummer, because, in my opinion, if you're using Xml without namespaces then you're just pushing around less-than/greater-than delimited files. Considering that so much effort was put into making schemas for most (all?) the ASP.NET config files and such, it's a shame if a control shipped without support for Xml Namespaces.

这真是令人um舌,因为我认为,如果您使用的是不带名称空间的Xml那么您所使用的只是小于/大于分隔符的文件。 考虑到为大多数(全部?)ASP.NET配置文件创建架构付出了很多努力,因此,如果控件出厂时不支持Xml命名空间,这是一个耻辱。

We're making the book very approachable for the beginner and intermediate dev, but there will be call-outs with gotchas like this that will hopefully save the advanced developer a lot of time. Also, if you're an advanced 1.1 dev, there is a lot of direct "it used to work like this, be careful because now..." exploration. I hope it'll save you time. It should be in bookstores just before ASP.NET 2.0 itself is.

我们对于初学者和中级开发人员来说,这本书非常容易上手,但是这样的标注会出现一些问题,希望可以为高级开发人员节省大量时间。 另外,如果您是1.1版的高级开发人员,那么可以直接进行很多“它曾经这样工作,请谨慎,因为现在...”的探索。 希望可以节省您的时间。 它应该在ASP.NET 2.0本身之前的书店中。

翻译自: https://www.hanselman.com/blog/aspnet-20-xmldatasources-xpath-doesnt-support-namespaces

xmldatasource

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值