GridView控件绑定到XML [英] Gridview binding to XML

查看:91
本文介绍了GridView控件绑定到XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让被绑定到一个简单的XML文档的简单网格视图,但我一定是失去了一些东西,因为我不断收到错误消息:

I am trying to make a simple grid view that is binded to a simple xml document but I must be missing something since I am keep getting error message:

与ID为GridView的数据源
  GridView1没有任何
  属性或从中属性
  生成列。确保您的
  数据源有内容。

The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

code

<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
    </Columns>
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
    DataFile="Notifications.xml" XPath="/data/node"></asp:XmlDataSource>

XML

<?xml version="1.0" encoding="utf-8" ?>
<data>
  <node>
    <id>1096</id>
    <name>About Us</name>
    <date>21/12/2009 17:03:43</date>
    <user id="1">writer</user>
  </node>
  <node>
    <id>1099</id>
    <name>News</name>
    <date>21/12/2009 17:03:47</date>
    <user id="1">writer</user>
  </node>
  <node>
    <id>1098</id>
    <name>Another page</name>
    <date>21/12/2009 17:03:52</date>
    <user id="1">writer</user>
  </node>
</data>

这是我的也许XPath的,这是错误还是我做的东西在这里根本错了?

Is it perhaps my xpath that is wrong or am I making something fundamentally wrong here?

推荐答案

有多种方法来得到这个工作:

There are a number of ways to get this to work:


  1. 使用Brian的解决方案,这是重写XML来使用属性,而不是子节点。

  2. 使用XSLT转换到子节点动态转换为属性。见<一href=\"http://stackoverflow.com/questions/875900/move-sub-nodes-into-parent-attributes-with-xslt\">this SO质疑为可以执行操作的XSLT。

  3. XML数据加载到DataSet,它在内部做这种转换。

  1. Use Brian's solution, which is to rewrite the XML to use attributes instead of sub-nodes.
  2. Use an XSLT transform to dynamically convert the child nodes to attributes. See this SO question for an XSLT that can perform that operation.
  3. Load the XML data into a DataSet, which internally does this conversion.

下面是如何做一个例子3:

Here's an example of how to do #3:

DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/App_Data/mydata.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();

这最后一种方法的限制是,你不会自动绑定,你会与一个数据源控件。然而,由于是的XmlDataSource只读反正控制,这不一定是严重的限制。

The limitation of this last approach is that you don't get automatic databinding as you would with a data source control. However, since the XmlDataSource is a read-only control anyway, that's not necessarily a serious limitation.

这篇关于GridView控件绑定到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆