2010-03-07 35 views
0

我想在ASP中使用XML作爲數據源,然後將其顯示爲數據網格。該XML格式如下:ASP.NET 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> 

請幫助。提前致謝。

+0

您需要向我們展示更多代碼,gridview綁定和您正在使用的實際數據源(XmlDataSource也許?) – Pharabus

+0

我添加了我的頁面代碼。希望這有助於。再次感謝。 –

回答

0

gridview不會選擇它的元素作爲屬性而不是屬性(並且樹視圖控件可以處理元素而不是屬性)。你可以推薦你的xml使用屬性,或者在代碼隱藏中將gridview綁定到數據集(使用DataSet的ReadXml方法)

我看到了一些建議,將autogeneratecolumns設置爲false並使用綁定字段,但我沒有設法得到那個工作。

0

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

該示例很有意義,因爲GridView需要某種方式來派生列標題。你的XML文件不提供這個。另外,它不包含節點中的任何值。

我也建議嘗試Repeater控件,因爲它會給你更多的控制顯示內容,以及在XML文件的結構中更自由。有一個可愛的example here