Edit XML data through DataSet in C# : XML Edit « XML « ASP.Net






Edit XML data through DataSet in C#

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script Language="c#" runat="server">
  void Page_Load(object sender, EventArgs e)
  {
    DataSet objDataSet = new DataSet();
    // Read in the XML file
    objDataSet.ReadXml(Server.MapPath("NewEmployees.xml"));

    // Show it in a grid
    dgEmployees1.DataSource = objDataSet.Tables[0].DefaultView;
    dgEmployees1.DataBind();

    // Modify a row
    objDataSet.Tables["employee"].Rows[0]["firstName"] = "A";
    objDataSet.Tables["employee"].Rows[0]["lastName"] = "B";

    // Add a new row to the table
    DataTable objTable = null;
    DataRow objNewRow = null;
    objTable = objDataSet.Tables["employee"];
    objNewRow = objTable.NewRow();
    objNewRow["firstName"] = "C";
    objNewRow["lastName"] = "D";
    objTable.Rows.Add(objNewRow);

    // Save it to a new file
    objDataSet.WriteXml(Server.MapPath("Employees2.xml"));

    // Read in the new file
    DataSet objDataSet2 = new DataSet();
    objDataSet2.ReadXml(Server.MapPath("Employees2.xml"));

    // Show it in another grid
    dgEmployees2.DataSource = objDataSet2.Tables[0].DefaultView;
    dgEmployees2.DataBind();
  }
</script>
<html>
 <body>
  <table>
   <tr>
    <td valign="top"><asp:DataGrid id="dgEmployees1" runat="server" /></td>
    <td valign="top"><asp:DataGrid id="dgEmployees2" runat="server" /></td>
   </tr>
  </table>
 </body>
</html>


<%--NewEmployees.xml
<?xml version='1.0'?>
<employees>
  <employee id="1">    
      <firstName>Nancy</firstName>
      <lastName>Lee</lastName> 
    <city>Seattle</city>
    <state>WA</state>
    <zipCode>98122</zipCode>   
  </employee>
  <employee id="2">    
      <firstName>Jason</firstName>
      <lastName>Wang</lastName>
    <city>Vancouver</city>
    <state>WA</state>
    <zipCode>98123</zipCode>   
  </employee> 
</employees>

--%>




           
       








Related examples in the same category

1.Use asp datagrid to edit data in XML