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.
- Initialize dataset object.
- Create DataTable object.
- Create columns for the data table.
- Send data to the data table by row wise.
- Adding data table to the dataset.
- Creating XML file using dataset WriteXml() method.
- See output:
| Creating XML file using Dataset in C#.NET or ASP.NET |
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
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.