Saturday 23 August 2014

How to Create XML File Dynamically in Asp.Net using C#.

Dynamically created xml file using C#


Description:-



In this example we explain that how to create XML file Dynamically in asp.net or create XML runtime through C# code in asp.net.

We already explain that how to create text file with insert,update facility but how to create XML file dynamically and save this file in your project folder in asp.net with C#.
The real advantages of creating XML file is that XML is platform independent so it can easily used other platform once it was created. To create XML file here we use XMLTextWriter class.

to show Example of How to Display Gridview Row Detail in Javascript Popup click here Display Gridview ARow Detail in Javascript Popup

to show Example of how to Redirect to another aspx page in javascript click here Redirect to another aspx page in javascript

to show example of how to create timer or display clock in javascript click here create timer or display clock in javascript

 Default3.aspx:-

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to Create or Generate Dynamically XML file using C# .Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>
        Click button to Generate student xml file dynamically</h1>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </form>
</body>
</html>

 Default3.aspx.cs:-

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

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            string filename = "D:\\student.xml";

            //assigning file name of desired file we want to create.

            XmlDocument xmlDoc = new XmlDocument();

            //creating new XML document

            XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);

            //creating XmlTestWriter, and passing file name and encoding type as argument

            xmlWriter.Formatting = Formatting.Indented;

            //setting XmlWriter formating to be indented

            xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

            //writing version and encoding type of XML in file.

            xmlWriter.WriteStartElement("Student");

            //writing first element

            xmlWriter.Close();

            //closing writer



            xmlDoc.Load(filename);

            //loading XML file



            XmlNode root = xmlDoc.DocumentElement;

            //creating child nodes.

            XmlElement childNode1 = xmlDoc.CreateElement("Stud_ID");

            XmlElement childNode2 = xmlDoc.CreateElement("Stud_Name");

            XmlElement childNode3 = xmlDoc.CreateElement("Stud_Country");
            XmlElement childNode4 = xmlDoc.CreateElement("Stud_Mobileno");


            //adding child node to root.

            root.AppendChild(childNode1);

            childNode1.InnerText = "1";

            //writing inner text of child node

            root.AppendChild(childNode2);

            childNode2.InnerText = "kirit";

            root.AppendChild(childNode3);

            childNode3.InnerText = "INDIA";
            root.AppendChild(childNode4);

            childNode3.InnerText = "9820420420";


            xmlDoc.Save(filename);

            //saving xml file

        }

        catch (Exception ex)
        {
            throw ex;


        }
    }
}

0 comments:

Post a Comment