Sunday, February 20, 2011

Adding new Record to Existing XML File using C-Sharp

1)     Creating Employee class with four properties EmpId, DeptId, EmpName, Location
2)     Reading an XML file into a DataTable
3)     Adding new record to above created Data table
4)     Creating xml file using Data table
namespace AddnewRecordtoExistinXMlFile
{
    public class Employee
    {
        public int EmpId
        {
            get;
            set;
        }
        public int DeptId
        {
            get;
            set;
        }
        public string  EmpName
        {
            get;
            set;
        }
        public string Location
        {
            get;
            set;
        }
     }

   class Program
    {
                
      // create object for Employee class
        Employee objEmp = new Employee();

        // <summary>
        /// methods for Creating an XML file from DataTable
        /// </summary>

        public void CreateXMlDoc()
        {
           
            DataTable dtEmployee = ReadXML("CreateEmployee.xml");

            // assigning values to Employee Object
            objEmp.EmpId = 2;
            objEmp.DeptId = 1;
            objEmp.EmpName = "XYZ";
            objEmp.Location = "Hyderabad";
         
     // adding Employee Object values to datarow.
            DataRow dr = dtEmployee.NewRow();
            dr["EmpId"] = objEmp.EmpId;
            dr["DeptId"] = objEmp.DeptId;
            dr["EmpName"] = objEmp.EmpName;
            dr["Location"] = objEmp.Location;
            dtEmployee.Rows.Add(dr);

            dtEmployee.WriteXml("CreateEmployee.xml");
         }


        // <summary>
        /// method for reading an XML file into a DataTable
        /// </summary>
        /// <param name="file">name (and path) of the XML file</param>
        /// <returns></returns>
        public DataTable ReadXML(string file)
        {
            //create the DataTable that will hold the data
            DataTable table = new DataTable("Employee");
            //create the table with the appropriate column names
            table.Columns.Add("EmpId", typeof(int));
            table.Columns.Add("DeptId", typeof(int));
            table.Columns.Add("TraderName", typeof(string));
            table.Columns.Add("EmpName", typeof(string));
            try
            {
                //open the file using a Stream

if (File.Exists(file))
{
   using (Stream stream = new FileStream(file, FileMode.Open,
   FileAccess.Read))
   {
   //use ReadXml to read the XML stream
   table.ReadXml(stream);
  //return the results
  }
}
 return table;
}
            catch (Exception ex)
            {
                return table;
            }
        }
        static void Main(string[] args)
        {
            Program p=new Program();
            p.PrepareXMlDoc();
            Console.ReadKey();
        }
    }


}

Note : the above scenario will work if and only if Exiting Xmlfile records and new record have the same structure, otherwise it will override with new record, and the existing records in the Xmlfile will be lost.

Sample Xml file
  <?xml version="1.0" standalone="yes" ?>
<DocumentElement>
<Employee>
  <EmpId>1</ EmpId>
  <DeptId>1</ DeptId>
  <EmpName>John</ EmpName>
  < Location>Hyderabad</ Location>
   </ Employee >
  < Employee >
  < EmpId >2</ EmpId >
  < DeptId >2</ DeptId >
  < EmpName>Harry</EmpName>
  <Location>Bangalore</ Location>
</ Employee >
</DocumentElement>

No comments:

Post a Comment