Pages

Tuesday 11 September 2012

How to create XML File from SQL Server Database in ASP.Net(Example-1)?

In this example I am going to describe you how to generate an XML File in ASP.Net. Here, I am generating XML File of the Customer Details.
Step-1: Create a Table(Customer) in your Database and add three columns(CustomerID ,Name, Address) to it.
Step-2: Insert some default values to it.

Step-3: Add a Page to your website for generating XML. Also add a button(GenerateXML) to the Page. Add a Namespace- "using System.Xml;".

Step-4: Write these following lines in the button's Click_Event.

    protected void GenerateXML_Click(object sender, EventArgs e)
    {
        //************ Retriving Customer Details From Database **************//
        SqlConnection con = new SqlConnection("--------- Your Connection String -------");
        SqlCommand com = new SqlCommand("select * from Customer", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        dt.TableName = "Customers";
        da.Fill(dt);
        //******************************************************************//
        dt.WriteXml(Server.MapPath("Customer.xml"));
    }

Here, I have generated and stored the XML File in the Solution Folder itself. You can give your own Location for saving the generated XML File.
Step-5: Now, Run your webpage and Click the button to generate XML File. After that, an XML File ("Customer.xml") will be generated in your given Folder. In my case it will be stored in my Solution Folder.

The Customer.xml 




Regards,
Prajanuranjan....


No comments:

Post a Comment

Total Pageviews