2011-07-26 45 views
1

這是我的xml文件如何綁定到XML GridView的

<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 

每當我運行此代碼集,我會收到一條錯誤消息,指出「在選定的數據源上找不到名爲」應對技能「的字段或屬性。 anyoen知道如何解決它?如果沒有任何教程或指南可用

回答

0

我猜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();