Thursday, March 24, 2011

Adding and updating column to an Existing XML file in C#

// creating dataset
DataSet ds = new DataSet();
       
// Reading XML file through ReadXml method which reads XML file and converts into Data Tables
ds.ReadXml("XMLFilePath");

// adding Required Column to an existing Table in dataset, here I am assuming my table is in '0' position in Dataset.
 ds.Tables[0].Columns.Add("ColumnName", typeof(string));

 // Here assigning value to Added Column
 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  {
   ds.Tables[0].Rows[i]["ColumnName"] = "Test";
  }

// writing dataset to xml file, xml file will be update with new column (Recently Added)
ds.WriteXml("XMLFilePath");


No comments:

Post a Comment