如何让GridView用XmlDataSource作为数据源来显示数据
想用XmlDataSource1作为数据来源,和GridView绑定显示数据。不论我怎么设置XmlDataSource控件的XPath属性,都不成功。Edit Column -> BoundField -> DataField, 没有任何东西显示在下拉框中。
请大家帮助看看如何设置能够让一个GridView用XmlDataSource作为数据源来显示数据?多谢!
我的代码如下,也请帮助看看错在哪里?谢谢!
<asp:GridView ID="CcrcGridView" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="658px"
DataSourceID="XmlDataSource1" >
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/ccrc.xml"
XPath="ProviderData"></asp:XmlDataSource>
我的xml文本格式如下:
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<ProviderData>
<Name>Resources Ltd.</Name>
<Address1>456 Second Avenue North</Address1>
<Address2 />
<City>Williams Lake</City>
<Zip>V2G 2A1</Zip>
<Country>CA</Country>
<Phone>250-456-1000</Phone>
<Website>www.bc.ca</Website>
</ProviderData>
<ProviderData>
<Name>Airline Company</Name>
<Address1>345 Airline Road</Address1>
<Address2 />
<City>Muskegan</City>
<Zip>49444</Zip>
<Country>CA</Country>
<Phone>231-333-7523</Phone>
<Website>http://www.airline.com/</Website>
</ProviderData>
<ProviderData>
<Name>The Arc of dba Howard Training Center</Name>
<Address1>352 Stonum Road</Address1>
<Address2>Test</Address2>
<City>Alameda</City>
<Zip>34567</Zip>
<Country>CA</Country>
<Phone>209-123-4000</Phone>
<Website>http://www.teset.com/coms2/dnbcompany_d9qsrl</Website>
</ProviderData>
<ProviderData>
<Name>Organization X</Name>
<Address1>X Street</Address1>
<Address2>Suite 100</Address2>
<City>Edmonton</City>
<Zip>T7J 4E0</Zip>
<Country>CA</Country>
<Phone>780-345-3456</Phone>
<Website>google.ca</Website>
</ProviderData>
</DocumentElement>
[解决办法]
<body>
<form id="form1" runat="server">
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/ccrc.xml" XPath="/DocumentElement/ProviderData">
</asp:XmlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#XPath("Name") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%#XPath("Phone") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
[解决办法]
实在不行,据把XML放进DataTable中,再绑定