XmlDataSource 控件使得 XML 数据可用于数据绑定控件,可以使用该控件同时显示分层数据和表格数据,多数用于显示只读方案中的分层 XML 数据。
Example.aspx
.....
<div id="gvDataDiv">
<asp:GridView ID="gvData" AutoGenerateColumns="false" runat="server"
DataSourceID="xmlData">
<Columns>
<asp:TemplateField HeaderText="项目编号">
<ItemTemplate >
<asp:Label runat="server" Text='<%#XPath ("projID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="标的名称">
<ItemTemplate>
<asp:Label runat="server" Text='<%#XPath("projName") %>' />
</ItemTemplate>
</asp:TemplateField>
... ...
<asp:TemplateField HeaderText="增加">
<ItemTemplate>
<asp:LinkButton ID="addClick" runat="server" CommandName="addBtn" >
<asp:Image ID="addBtnImage" runat="server" ImageUrl="~/Images/addBtn.png" />
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
... ....
</Columns>
</asp:GridView>
<asp:XmlDataSource ID="xmlData" DataFile="/App_Data/projContext.xml"XPath="/Projects/ProjContext" runat="server" />
projContext.xml
<?xml version="1.0" encoding="utf-8" ?>
<Projects>
<ProjContext>
<projID>111</projID>
<projName>大学</projName>
<projPrice>1222</projPrice>
<projDate>2013-07-04</projDate>
</ProjContext>
<ProjContext>
<projID>222</projID>
<projName>中科</projName>
<projPrice>12</projPrice>
<projDate>2013-07-10</projDate>
</ProjContext>
</Projects>