I am trying to use an XML as a datasource开发者_运维百科 in ASP and then display it as a datagrid. The XML has the following format:
<?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>
When I try to run the simple page I receive the following error
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.
Here is my page code
<%@ 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>
Please help. Thanks in advance.
The gridview wont pick up elements for columns it works off attributes instead (and wierdly the treeview control works off elements not attributes). You can either ammend your xml to use attributes or bind the gridview to a dataset in the codebehind instead (using the ReadXml method of the DataSet)
I have seen some suggestions that setting autogeneratecolumns to false and using bound fields works but I have not managed to get that working.
Your XML source file is not in the format that the XmlDataSource expects. Please see this example.
The example makes sense since the GridView needs some way to derive the column headers. Your XML file does not provide this. As an aside, it does not contain any values in the nodes.
I would also suggest trying the Repeater control, as it will give you a bit more control over what the display, as well as being more liberal in the structure of the XML file. There is a lovely example here.
精彩评论