Add Textbox values to Xml Element/Node

    • Add Textbox values to Xml Element/Node

      Hello,
      I've already done the work load.. but its not what i wanted
      The thing is that, the value input from the textboxes are stored in the xml file

      I have this Xml file structure:

      Source Code

      1. <Contacts xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
      2. <Contact>
      3. <FirstName>sample</FirstName>
      4. <MidName>sample</MidName>
      5. <LastName>sample</LastName>
      6. <Age>20</Age>
      7. <Address>
      8. <City>sample</City>
      9. <Street>sample</Street>
      10. <State>sample</State>
      11. <Country>sample</Country>
      12. </Address>
      13. <Phone>
      14. <Mobile>1234567</Mobile>
      15. <Home>7654321</Home>
      16. <Office>7894561</Office>
      17. </Phone>
      18. <Company>
      19. <Name>sample</Name>
      20. <Department>IT Technician</Department>
      21. </Company>
      22. <Email>xyz@xxx.com</Email>
      23. </Contact>
      24. </Contacts>
      Display All


      but in my output, i'm getting this:

      Source Code

      1. <Contacts>
      2. <contact>
      3. <FirstName>sample</FirstName>
      4. <MidName>sample</MidName>
      5. <LastName>sample</LastName>
      6. <Age>20</Age>
      7. <City>sample</City>
      8. <Street>sample</Street>
      9. <State>sample</State>
      10. <Country>sample</Country>
      11. <Mobile>1234567</Mobile>
      12. <Home>7894561</Home>
      13. <Office>1122334</Office>
      14. <Company>sample</Company>
      15. <Name>sample</Name>
      16. <Department>IT Technician</Department>
      17. <Email>xyz@xxx.com</Email>
      18. </contact>
      19. <Contacts>
      Display All


      Here's my Interface:

      Source Code

      1. <asp:GridView ID="gvProducts" AutoGenerateColumns="False" CssClass="GridViewStyle"
      2. runat="server" OnRowCommand="gvProducts_RowCommand" OnRowEditing="gvProducts_RowEditing"
      3. OnRowCancelingEdit="gvProducts_RowCancelingEdit" OnRowUpdating="gvProducts_RowUpdating"
      4. OnRowDeleting="gvProducts_RowDeleting" AllowPaging="True"
      5. AllowSorting="True" >
      6. <RowStyle CssClass="RowStyle" />
      7. <FooterStyle CssClass="RowStyle" />
      8. <SelectedRowStyle CssClass="SelectedRowStyle" />
      9. <HeaderStyle CssClass="HeaderStyle" />
      10. <AlternatingRowStyle CssClass="AltRowStyle" />
      11. <Columns>
      12. <asp:TemplateField HeaderText="First Name">
      13. <ItemTemplate>
      14. <%#Eval("FirstName")%></ItemTemplate>
      15. <FooterTemplate>
      16. <asp:TextBox ID="txtNewFname" runat="Server" />
      17. </FooterTemplate>
      18. <EditItemTemplate>
      19. <asp:TextBox ID="txtFname" Text='<%#Eval("FirstName")%>' runat="server" />
      20. </EditItemTemplate>
      21. </asp:TemplateField>
      22. <asp:TemplateField HeaderText="Mid Name">
      23. <ItemTemplate>
      24. <%#Eval("MidName")%></ItemTemplate>
      25. <FooterTemplate>
      26. <asp:TextBox ID="txtNewMname" runat="Server" />
      27. </FooterTemplate>
      28. <EditItemTemplate>
      29. <asp:TextBox ID="txtMname" Text='<%#Eval("MidName")%>' runat="server" />
      30. </EditItemTemplate>
      31. </asp:TemplateField>
      32. <asp:TemplateField HeaderText="Last Name">
      33. <ItemTemplate>
      34. <%#Eval("LastName")%></ItemTemplate>
      35. <EditItemTemplate>
      36. <asp:TextBox ID="txtLname" Text='<%#Eval("LastName")%>' runat="server" />
      37. </EditItemTemplate>
      38. <FooterTemplate>
      39. <asp:TextBox ID="txtNewLname" runat="server" />
      40. </FooterTemplate>
      41. </asp:TemplateField>
      42. <asp:TemplateField HeaderText="Age">
      43. <ItemTemplate>
      44. <%#Eval("Age")%>
      45. </ItemTemplate>
      46. <EditItemTemplate>
      47. <asp:TextBox ID="txtAge" Text='<%#Eval("Age")%>' runat="server" />
      48. </EditItemTemplate>
      49. <FooterTemplate>
      50. <asp:TextBox ID="txtNewAge" runat="server" />
      51. </FooterTemplate>
      52. </asp:TemplateField>
      53. <asp:TemplateField HeaderText="City">
      54. <ItemTemplate>
      55. <%#Eval("City")%></ItemTemplate>
      56. <FooterTemplate>
      57. <asp:TextBox ID="txtNewCity" runat="Server" />
      58. </FooterTemplate>
      59. <EditItemTemplate>
      60. <asp:TextBox ID="txtCity" Text='<%#Eval("City")%>' runat="server" />
      61. </EditItemTemplate>
      62. </asp:TemplateField>
      63. <asp:TemplateField HeaderText="Street">
      64. <ItemTemplate>
      65. <%#Eval("Street")%></ItemTemplate>
      66. <FooterTemplate>
      67. <asp:TextBox ID="txtNewStreet" runat="Server" />
      68. </FooterTemplate>
      69. <EditItemTemplate>
      70. <asp:TextBox ID="txtStreet" Text='<%#Eval("Street")%>' runat="server" />
      71. </EditItemTemplate>
      72. </asp:TemplateField>
      73. <asp:TemplateField HeaderText="State">
      74. <ItemTemplate>
      75. <%#Eval("State")%></ItemTemplate>
      76. <FooterTemplate>
      77. <asp:TextBox ID="txtNewState" runat="Server" />
      78. </FooterTemplate>
      79. <EditItemTemplate>
      80. <asp:TextBox ID="txtState" Text='<%#Eval("State")%>' runat="server" />
      81. </EditItemTemplate>
      82. </asp:TemplateField>
      83. <asp:TemplateField HeaderText="Country">
      84. <ItemTemplate>
      85. <%#Eval("Country")%></ItemTemplate>
      86. <FooterTemplate>
      87. <asp:TextBox ID="txtNewCountry" runat="Server" />
      88. </FooterTemplate>
      89. <EditItemTemplate>
      90. <asp:TextBox ID="txtCountry" Text='<%#Eval("Country")%>' runat="server" />
      91. </EditItemTemplate>
      92. </asp:TemplateField>
      93. <asp:TemplateField HeaderText="Mobile No.">
      94. <ItemTemplate>
      95. <%#Eval("Mobile")%></ItemTemplate>
      96. <FooterTemplate>
      97. <asp:TextBox ID="txtNewMobile" runat="Server" />
      98. </FooterTemplate>
      99. <EditItemTemplate>
      100. <asp:TextBox ID="txtMobile" Text='<%#Eval("Mobile")%>' runat="server" />
      101. </EditItemTemplate>
      102. </asp:TemplateField>
      103. <asp:TemplateField HeaderText="Home No.">
      104. <ItemTemplate>
      105. <%#Eval("Home")%></ItemTemplate>
      106. <FooterTemplate>
      107. <asp:TextBox ID="txtNewHome" runat="Server" />
      108. </FooterTemplate>
      109. <EditItemTemplate>
      110. <asp:TextBox ID="txtHome" Text='<%#Eval("Home")%>' runat="server" />
      111. </EditItemTemplate>
      112. </asp:TemplateField>
      113. <asp:TemplateField HeaderText="Office No.">
      114. <ItemTemplate>
      115. <%#Eval("Office")%></ItemTemplate>
      116. <FooterTemplate>
      117. <asp:TextBox ID="txtNewOffice" runat="Server" />
      118. </FooterTemplate>
      119. <EditItemTemplate>
      120. <asp:TextBox ID="txtOffice" Text='<%#Eval("Office")%>' runat="server" />
      121. </EditItemTemplate>
      122. </asp:TemplateField>
      123. <asp:TemplateField HeaderText="Company">
      124. <ItemTemplate>
      125. <%#Eval("Company")%></ItemTemplate>
      126. <FooterTemplate>
      127. <asp:TextBox ID="txtNewCompany" runat="Server" />
      128. </FooterTemplate>
      129. <EditItemTemplate>
      130. <asp:TextBox ID="txtCompany" Text='<%#Eval("Company")%>' runat="server" />
      131. </EditItemTemplate>
      132. </asp:TemplateField>
      133. <asp:TemplateField HeaderText="Company Name">
      134. <ItemTemplate>
      135. <%#Eval("Name")%></ItemTemplate>
      136. <FooterTemplate>
      137. <asp:TextBox ID="txtNewName" runat="Server" />
      138. </FooterTemplate>
      139. <EditItemTemplate>
      140. <asp:TextBox ID="txtName" Text='<%#Eval("Name")%>' runat="server" />
      141. </EditItemTemplate>
      142. </asp:TemplateField>
      143. <asp:TemplateField HeaderText="Department">
      144. <ItemTemplate>
      145. <%#Eval("Department")%></ItemTemplate>
      146. <FooterTemplate>
      147. <asp:TextBox ID="txtNewDepartment" runat="Server" />
      148. </FooterTemplate>
      149. <EditItemTemplate>
      150. <asp:TextBox ID="txtDepartment" Text='<%#Eval("Department")%>' runat="server" />
      151. </EditItemTemplate>
      152. </asp:TemplateField>
      153. <asp:TemplateField HeaderText="E-mail Address">
      154. <ItemTemplate>
      155. <%#Eval("Email")%></ItemTemplate>
      156. <FooterTemplate>
      157. <asp:TextBox ID="txtNewEmail" runat="Server" />
      158. </FooterTemplate>
      159. <EditItemTemplate>
      160. <asp:TextBox ID="txtEmail" Text='<%#Eval("Email")%>' runat="server" />
      161. </EditItemTemplate>
      162. </asp:TemplateField>
      163. <asp:TemplateField>
      164. <ItemTemplate>
      165. <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" />
      166. </ItemTemplate>
      167. <EditItemTemplate>
      168. <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update" />
      169. <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel" />
      170. <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" />
      171. </EditItemTemplate>
      172. <FooterTemplate>
      173. <asp:LinkButton ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert"
      174. UseSubmitBehavior="False" />
      175. </FooterTemplate>
      176. </asp:TemplateField>
      177. </Columns>
      178. </asp:GridView>
      179. <br />
      180. <asp:LinkButton ID="btnAdd" runat="server" Text="Add" OnClick="AddNewRecord" />
      Display All


      Here's my Code Behind in C#:

      Source Code

      1. using System;
      2. using System.Data;
      3. using System.Collections.Generic;
      4. using System.Linq;
      5. using System.Web;
      6. using System.Web.UI;
      7. using System.Web.UI.WebControls;
      8. using System.Xml;
      9. public partial class xmlContact1 : System.Web.UI.Page
      10. {
      11. protected void Page_Load(object sender, EventArgs e)
      12. {
      13. if (!Page.IsPostBack)
      14. BindData();
      15. }
      16. private void BindData()
      17. {
      18. //bind the gridview
      19. gvProducts.DataSource = RetrieveProducts();
      20. gvProducts.DataBind();
      21. }
      22. private DataSet RetrieveProducts()
      23. {
      24. //if the products data is already saved in viewstate
      25. //return that data
      26. if (ViewState["Contacts"] != null)
      27. return ViewState["Contacts"] as DataSet;
      28. DataSet dsProducts = new DataSet();
      29. //read the xml schema
      30. //dsProducts.ReadXmlSchema(Server.MapPath(@"XML\Contacts.xsd"));
      31. //read the xml data
      32. dsProducts.ReadXml(Server.MapPath(@"XML\Contacts.xml"));
      33. //store the data into viewstate which will be used for
      34. //subsequent postbacks
      35. ViewState["Contacts"] = dsProducts;
      36. return dsProducts;
      37. }
      38. protected void AddNewRecord(object sender, EventArgs e)
      39. {
      40. gvProducts.ShowFooter = true;
      41. BindData();
      42. }
      43. protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
      44. {
      45. // If multiple ButtonField column fields are used, use the
      46. // CommandName property to determine if "Insert" button was clicked.
      47. if (e.CommandName.Equals("Insert"))
      48. {
      49. //fetch the values of the new product
      50. TextBox txtNewFname = gvProducts.FooterRow.FindControl("txtNewFname") as TextBox;
      51. TextBox txtNewMname= gvProducts.FooterRow.FindControl("txtNewMname") as TextBox;
      52. TextBox txtNewLname= gvProducts.FooterRow.FindControl("txtNewLname") as TextBox;
      53. TextBox txtNewAge = gvProducts.FooterRow.FindControl("txtNewAge") as TextBox;
      54. TextBox txtNewCity = gvProducts.FooterRow.FindControl("txtNewCity") as TextBox;
      55. TextBox txtNewStreet = gvProducts.FooterRow.FindControl("txtNewStreet") as TextBox;
      56. TextBox txtNewState = gvProducts.FooterRow.FindControl("txtNewState") as TextBox;
      57. TextBox txtNewCountry = gvProducts.FooterRow.FindControl("txtNewCountry") as TextBox;
      58. TextBox txtNewMobile = gvProducts.FooterRow.FindControl("txtNewMobile") as TextBox;
      59. TextBox txtNewHome = gvProducts.FooterRow.FindControl("txtNewHome") as TextBox;
      60. TextBox txtNewOffice = gvProducts.FooterRow.FindControl("txtNewOffice") as TextBox;
      61. TextBox txtNewCompany = gvProducts.FooterRow.FindControl("txtNewCompany") as TextBox;
      62. TextBox txtNewName = gvProducts.FooterRow.FindControl("txtNewName") as TextBox;
      63. TextBox txtNewDepartment = gvProducts.FooterRow.FindControl("txtNewDepartment") as TextBox;
      64. TextBox txtNewEmail = gvProducts.FooterRow.FindControl("txtNewEmail") as TextBox;
      65. //insert the new product
      66. InsertProduct(txtNewFname.Text, txtNewMname.Text, txtNewLname.Text, txtNewAge.Text, txtNewCity.Text, txtNewStreet.Text, txtNewState.Text, txtNewCountry.Text, txtNewMobile.Text, txtNewHome.Text, txtNewOffice.Text, txtNewCompany.Text, txtNewName.Text, txtNewDepartment.Text, txtNewEmail.Text);
      67. //hide the footer
      68. gvProducts.ShowFooter = false;
      69. //clear the view state so that latest list will be retrieved from file
      70. ViewState["Contacts"] = null;
      71. // rebind the data
      72. BindData();
      73. }
      74. }
      75. private void InsertProduct(string FirstName, string MidName, string LastName, string Age, string City, string Street, string State, string Country, string Mobile, string Home, string Office, string Company, string Name, string Department, string Email)
      76. {
      77. //get the product data from viewstate
      78. DataSet dsProducts = ViewState["Contacts"] as DataSet;
      79. //create a new product row and populate it with user input data
      80. DataRow newProductRow = dsProducts.Tables[0].NewRow();
      81. newProductRow["FirstName"] = FirstName;
      82. newProductRow["MidName"] = MidName;
      83. newProductRow["LastName"] = LastName;
      84. newProductRow["Age"] = Age;
      85. newProductRow["City"] = City;
      86. newProductRow["Street"] = Street;
      87. newProductRow["State"] = State;
      88. newProductRow["Country"] = Country;
      89. newProductRow["Mobile"] = Mobile;
      90. newProductRow["Home"] = Home;
      91. newProductRow["Office"] = Office;
      92. newProductRow["Company"] = Company;
      93. newProductRow["Name"] = Name;
      94. newProductRow["Department"] = Department;
      95. newProductRow["Email"] = Email;
      96. dsProducts.Tables[0].Rows.Add(newProductRow);
      97. //write the data back to the XML file
      98. dsProducts.WriteXml(GetXMLSourcePath());
      99. //store the new product dataset into the viewstate
      100. ViewState["Products"] = dsProducts;
      101. BindData();
      102. }
      103. private string GetXMLSourcePath()
      104. {
      105. return Server.MapPath(@"XML\Contacts.xml");
      106. }
      107. protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
      108. {
      109. gvProducts.EditIndex = e.NewEditIndex;
      110. BindData();
      111. }
      112. protected void gvProducts_RowUpdating(Object sender, GridViewUpdateEventArgs e)
      113. {
      114. // Get the product id of the selected product
      115. string ID = gvProducts.DataKeys[e.RowIndex].Value.ToString();
      116. // Get the GridViewRow object that represents the row being edited
      117. // from the Rows collection of the GridView control.
      118. GridViewRow row = gvProducts.Rows[e.RowIndex];
      119. // Get the controls that contain the updated values. In this example, the updated values are contained
      120. //in the TextBox controls declared in the edit item templates of each TemplateField
      121. // column fields in the GridView control.
      122. TextBox txtFname= (TextBox)row.FindControl("txtFname");
      123. TextBox txtMname= (TextBox)row.FindControl("txtMname");
      124. TextBox txtLname= (TextBox)row.FindControl("txtLname");
      125. TextBox txtAge= (TextBox)row.FindControl("txtAge");
      126. TextBox txtCity = (TextBox)row.FindControl("txtCity");
      127. TextBox txtStreet = (TextBox)row.FindControl("txtStreet");
      128. TextBox txtState = (TextBox)row.FindControl("txtState");
      129. TextBox txtCountry = (TextBox)row.FindControl("txtCountry");
      130. TextBox txtMobile = (TextBox)row.FindControl("txtMobile");
      131. TextBox txtHome = (TextBox)row.FindControl("txtHome");
      132. TextBox txtOffice = (TextBox)row.FindControl("txtOffice");
      133. TextBox txtCompany = (TextBox)row.FindControl("txtCompany");
      134. TextBox txtName = (TextBox)row.FindControl("txtName");
      135. TextBox txtDepartment = (TextBox)row.FindControl("txtDepartment");
      136. TextBox txtEmail = (TextBox)row.FindControl("txtEmail");
      137. //update the product row
      138. DataSet dsProducts = ViewState["Contacts"] as DataSet;
      139. dsProducts.Tables[0].Rows[e.RowIndex]["FirstName"] = txtFname.Text;
      140. dsProducts.Tables[0].Rows[e.RowIndex]["MidName"] = txtMname.Text;
      141. dsProducts.Tables[0].Rows[e.RowIndex]["LastName"] = txtLname.Text;
      142. dsProducts.Tables[0].Rows[e.RowIndex]["Age"] = txtAge.Text;
      143. dsProducts.Tables[0].Rows[e.RowIndex]["City"] = txtCity.Text;
      144. dsProducts.Tables[0].Rows[e.RowIndex]["Street"] = txtStreet.Text;
      145. dsProducts.Tables[0].Rows[e.RowIndex]["State"] = txtState.Text;
      146. dsProducts.Tables[0].Rows[e.RowIndex]["Country"] = txtCountry.Text;
      147. dsProducts.Tables[0].Rows[e.RowIndex]["Mobile"] = txtMobile.Text;
      148. dsProducts.Tables[0].Rows[e.RowIndex]["Home"] = txtHome.Text;
      149. dsProducts.Tables[0].Rows[e.RowIndex]["Office"] = txtOffice.Text;
      150. dsProducts.Tables[0].Rows[e.RowIndex]["Company"] = txtCompany.Text;
      151. dsProducts.Tables[0].Rows[e.RowIndex]["Name"] = txtName.Text;
      152. dsProducts.Tables[0].Rows[e.RowIndex]["Department"] = txtDepartment.Text;
      153. dsProducts.Tables[0].Rows[e.RowIndex]["Email"] = txtEmail.Text;
      154. //persiste changes back to the xml data file
      155. dsProducts.WriteXml(GetXMLSourcePath());
      156. //store the latest dataset into viewstate
      157. ViewState["Contacts"] = dsProducts;
      158. //set gridview back to normal mode
      159. gvProducts.EditIndex = -1;
      160. //rebind gridview with latest data
      161. BindData();
      162. }
      163. protected void gvProducts_RowDeleting(Object sender, GridViewDeleteEventArgs e)
      164. {
      165. //get the dataset stored from the viewstate
      166. DataSet dsProducts = ViewState["Contacts"] as DataSet;
      167. //delete the row from the dataset
      168. dsProducts.Tables[0].Rows[e.RowIndex].Delete();
      169. //write those changes to XML data file and save latest dataset to viewstate
      170. dsProducts.WriteXml(GetXMLSourcePath());
      171. ViewState["Contacts"] = dsProducts;
      172. //change gridview back to normal window
      173. gvProducts.EditIndex = -1;
      174. //rebind the gridview with latest data
      175. BindData();
      176. }
      177. protected void gvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      178. {
      179. gvProducts.EditIndex = -1;
      180. BindData();
      181. }
      182. }
      Display All


      Please help me to get this right
      Thank You