Creating XML file using Dataset in C#.NET or ASP.NET

Previous                                                        Home                                                 Next

Creating XML file using Dataset in C#.NET or ASP.NET

In this example we are creating the XML file using dataset object. For this please follow below steps. 
  1. Initialize dataset object. 
  2. Create DataTable object. 
  3. Create columns for the data table. 
  4. Send data to the data table by row wise. 
  5. Adding data table to the dataset. 
  6. Creating XML file using dataset WriteXml() method. 
  7. See output:
Creating XML file using Dataset in C#.NET or ASP.NET
Creating XML file using Dataset in C#.NET or ASP.NET
Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    //creating datatable object.
    DataTable objDataTable = new DataTable();
    public DataRow drData = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //creating the dataset object. 
        DataSet objDataset = new DataSet();

        //location of the new xml file
        string strFileName = @"c://customer.xml";


        //Creating columns for the datatable. 
        objDataTable.Columns.Add("CustomerId", Type.GetType("System.Int32"));
        objDataTable.Columns.Add("CustomerName", Type.GetType("System.String"));

        //insert data into DataTable
        FillDataToDataTable(1111, "Ranga Rajesh");
        FillDataToDataTable(1112, "Anil Kumar");
        FillDataToDataTable(1113, "Murthy");
        FillDataToDataTable(1114, "Sanjeev");

        objDataset.Tables.Add(objDataTable);

        objDataset.Tables[0].TableName = "Customer";

        objDataset.WriteXml(strFileName);
        Response.Write("XML File Created");
    }
    public void FillDataToDataTable(int CustId, string CustName)
    {
        drData = objDataTable.NewRow();
        drData["CustomerId"] = CustId;
        drData["CustomerName"] = CustName;
        objDataTable.Rows.Add(drData);
    }
}


Previous                                                        Home                                                 Next
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar