ASP.NET XML作为数据源错误

时间:2010-03-07 23:20:58

标签: asp.net xml datasource

我试图在ASP中使用XML作为数据源,然后将其显示为数据网格。 XML具有以下格式:

<?xml version="1.0" encoding="UTF-8"?>
<people type="array">
  <person>
    <id type="integer"></id>
    <first_name></first_name>
    <last_name></last_name>
    <title></title>
    <company></company>
    <tags>
    </tags>
    <locations>
      <location primary="false" label="work">
        <email></email>
        <website></website>
        <phone></phone>
        <cell></cell>
        <fax></fax>
        <street_1/>
        <street_2/>
        <city/>
        <state/>
        <postal_code/>
        <country/>
      </location>
    </locations>
    <notes></notes>
    <created_at></created_at>
    <updated_at></updated_at>
  </person>
</people>

当我尝试运行简单页面时,我收到以下错误

Server Error in '/' Application.
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.

这是我的页面代码

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="shout._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
            DataFile="~/App_Data/people.xml" XPath="people/person"></asp:XmlDataSource>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            DataSourceID="XmlDataSource1">
        </asp:GridView>

    </div>
    </form>
</body>
</html>

请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:0)

gridview不会为它取代属性的列拾取元素(而且树视图控件很好地处理元素而非属性)。您可以修改xml以使用属性或将gridview绑定到代码隐藏中的数据集(使用DataSet的ReadXml方法)

我已经看到一些建议将autogeneratecolumns设置为false并使用绑定字段有效,但我还没有设法让它工作。

答案 1 :(得分:0)

您的XML源文件不是XmlDataSource期望的格式。 Please see this example

这个例子很有意义,因为GridView需要一些方法来派生列标题。您的XML文件不提供此功能。另外,它不包含节点中的任何值。

我还建议尝试使用Repeater控件,因为它可以让您更好地控制显示内容,以及在XML文件结构中更自由。有一个可爱的example here