如何将XML绑定到gridview

本文关键字:gridview 绑定 XML | 更新日期: 2023-09-27 18:03:23

这是我的xml文件

<ISPChecklist>
            <Domain name="Coping Skills">
            <Indicator>
                Client shows impovement in either 1 of the areas listed below and shows reduction in frequency of inapporiate coping behaviors :
                - anger management
                - ability to make concrete plans about his/her future
                - self percetion/self worth
                - expand internal locus of control.
            </Indicator>
            <AttainmentDate></AttainmentDate>
            <Remarks></Remarks>
        </Domain>
</ISPChecklist>

这是我的网格视图

<asp:GridView ID="ISPChecklist" runat="server" 
                OnRowDataBound="OnDataBound" 
                onselectedindexchanged="ISPChecklist_SelectedIndexChanged">
            <Columns>
            <asp:BoundField HeaderText="Domain" DataField= "Domain" />
            <asp:BoundField HeaderText="Indicator" DataField="Indicator" />
            <asp:TemplateField HeaderText="Date Of Attainment">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="TB_Date" Columns="5"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Remarks">
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="TB_Remarks" Columns="5"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            </asp:GridView>

这是我的。cs文件

DataSet checklistDataSet;
string filePath = Server.MapPath("clientISPChecklist.xml");
        checklistDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        checklistDataSet.ReadXml(filePath);
        ISPChecklist.DataSource = checklistDataSet.Tables[0].DefaultView;
        ISPChecklist.DataBind();//errors occurs here

每当我运行这组代码时,我都会收到一条错误消息,指出"在选定的数据源上找不到名为'Coping Skills'的字段或属性"。有人知道怎么解吗?如果没有,有没有可用的教程或指南

如何将XML绑定到gridview

使用XMLDataSource和XPath()方法

我想Linq-XML是更好的选择。

XDocument doc = XDocument.Load(MapPath("~/filename.xml"));
var result = from n in doc.Descendants("Domain")
             select new
                {
                   Domain = n.HasAttributes ? n.Attribute("name").Value : "",
                   Indicator=n.Element("Indicator").Value,
                   AttainmentDate = n.Element("AttainmentDate").Value,
                   Remark=n.Element("AttainmentDate").Value 
               };
ISPChecklist.DataSource = result.ToList();
ISPChecklist.DataBind();