Thursday, 22 December 2011

how to create xml file using asp.net (retrive table and create xml file)

-------------------------------aspx page--------------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlwithproperelements1.aspx.cs" Inherits="xmlwithproperelements1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>


----------------------------aspx.cs page-----------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;

public partial class xmlwithproperelements1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=TECH-SWAPNILM\\SQLEXPRESS;Initial Catalog=swap;User ID=swapnil;Password=sa");
        con.Open();

        SqlCommand com = new SqlCommand("Select * from Employee", con);

        SqlDataReader reader = com.ExecuteReader(); ;

        XmlDocument doc = new XmlDocument();
        string xmlfile = "D:\\PaymentDetails1.xml";

        XmlDeclaration decln;
        XmlElement rootelem, parent, parent1, child0, child1, child2;

        if (!System.IO.File.Exists(xmlfile))
        {
            //XmlAttribute attr;

            decln = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            rootelem = doc.CreateElement("PaymentDetails4");

            doc.AppendChild(decln);
            doc.AppendChild(rootelem); //SupplierName,SupplierNumber...


            while (reader.Read())
            {
                // XmlElement rootelem, parent, parent1, child0, child1, child2, child3, child4, child5, child6,
                //child7, child8, child9, child10, child11, child12, child13, child14, child15, child16;

                parent = doc.CreateElement("H");
                //attr = doc.CreateAttribute("SupplierName");
                child0 = doc.CreateElement("id");
                child1 = doc.CreateElement("Ename");
                child2 = doc.CreateElement("Address");
               

                parent1 = doc.CreateElement("D");


                rootelem.AppendChild(parent);
                //parent.Attributes.Append(attr);
                parent.AppendChild(child0);
                parent.AppendChild(child1);
                parent.AppendChild(child2);
               


                //attr.Value = reader[0].ToString();
                child0.InnerText = reader[0].ToString();
                child1.InnerText = reader[1].ToString();
                child2.InnerText = reader[2].ToString();
             
            }

            doc.Save(xmlfile);

        }
        else
        {
            doc.Load(xmlfile);

            rootelem = doc.DocumentElement;


            //doc.AppendChild(decln);
            doc.AppendChild(rootelem);

            while (reader.Read())
            {
                //    XmlElement rootelem, parent, parent1, child0, child1, child2, child3, child4, child5, child6,
                //  child7, child8, child9, child10, child11, child12, child13, child14, child15, child16;

                parent = doc.CreateElement("H");
                //attr = doc.CreateAttribute("SupplierName");
                child0 = doc.CreateElement("id");
                child1 = doc.CreateElement("Ename");
                child2 = doc.CreateElement("Address");
              

                parent1 = doc.CreateElement("D");


                rootelem.AppendChild(parent);
                //parent.Attributes.Append(attr);
                parent.AppendChild(child0);
                parent.AppendChild(child1);
                parent.AppendChild(child2);
              


                //attr.Value = reader[0].ToString();
                child0.InnerText = reader[0].ToString();
                child1.InnerText = reader[1].ToString();
                child2.InnerText = reader[2].ToString();
         }


            doc.Save(xmlfile);

        }
       
    }
}