Monday, April 11, 2011

Create XML file Dynamically Using ASP.Net C# On Server

Code in aspx
                        <asp:TextBox runat="server" ID="txtFirstName" Text="manoranjan"></asp:TextBox>                   
                        <asp:TextBox runat="server" ID="txtLastName" Text="chennai"></asp:TextBox></td>

Code in aspx.cs

 string strMyXml = "<Employee>"+

        XmlDocument xDoc = new XmlDocument();
        //Here the xml is prepared and loaded in xml DOM.

        xDoc.Save(Server.MapPath("myNewXmlPerson.xml"));//instead of myNewXmlPerson , u can give any name.
        //The above line will save your XML.

        //This is one way to create xml. This method may not be very efficient to manage 500 data
        //You can go for using XmlDom instead to create xmlNodes.] Let me know if you want follow
        //xmlDom approach. To see its example you can read my blog post at

        //Now if u want to read this xml and get data from it and assign their value to text box you can do like this

        txtFirstName.Text = "";
        txtLastName.Text = "";
        XmlDocument xDocRead = new XmlDocument();//I have taken this new object for your better understanding.


        XmlNode xFN = xDocRead.SelectSingleNode("Employee/firstName");
        XmlNode xLN = xDocRead.SelectSingleNode("Employee/lastName");

        txtFirstName.Text = xFN.InnerText;
        txtLastName.Text = xLN.InnerText;
1. Fill up dataset with query result:

    adp.Fill(ds, "Attendance");
2. Then create XmlDataDocument with dataset:
    XmlDataDocument xmlDoc = new XmlDataDocument(ds);

3. Finally you can save XmlDataDocument as xml file on server:


Let me know if the above example works for you, if you have any dobt let me know.

1 comment:

  1. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#,, php tutorial, Ms sql server

    Thanks a lot..!