Creating XML File Programmatically using Asp.Net C# VB.Net

Due to the popularity of XML file most of the times we need to create XML file in runtime. In this post i will show you a simple example how we can create XML file programmatically. By using this example you can also make XML file dynamically from SQL Server Database table by iterating across the rows. To make the XML on the fly first add System.Xml namespace in your project. Then create a declaration, a root element first and create elements sequentially. The example code is given below:

C# Code:

    protected void Page_Load(object sender, EventArgs e)
        XmlDocument writer = new XmlDocument();
        // Create XML declaration
        XmlNode declaration = writer.CreateNode(XmlNodeType.XmlDeclaration, null, null);

        // Make the root element first
        XmlElement root = writer.CreateElement("Article");

        //Creating the <> element
        XmlElement aspnet = writer.CreateElement("");

        //Creating attributes of <> element
        XmlAttribute id = writer.CreateAttribute("Id");
        id.Value = "0001";

        XmlAttribute title = writer.CreateAttribute("Title");
        title.InnerText = "Bind Dropdown List";

        XmlAttribute visit = writer.CreateAttribute("Visit");
        visit.InnerText = "985";

        XmlAttribute modified = writer.CreateAttribute("Modified");
        modified.InnerText = "Jan 01, 2009";

        // Now save/write the XML file into server root location
        //Server.MapPath(".") is used to find the root path
        writer.Save(Server.MapPath(".") + "//XMLFile.xml");

        Response.Write("File Creadted: " + Server.MapPath(".") + "//XMLFile.xml");

Note: Don’t forget to add “using System.Xml;” namespace.

VB.Net Code:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim writer As new XmlDocument
        'Create XML declaration
        Dim declaration As XmlNode

        declaration = writer.CreateNode(XmlNodeType.XmlDeclaration, Nothing, Nothing)

        'Make the root element first
        Dim root As XmlElement
        root = writer.CreateElement("Article")

        'Creating the <> element
        Dim aspnet As XmlElement
        aspnet = writer.CreateElement("")

        'Creating attributes of <> element
        Dim id As XmlAttribute
        id = writer.CreateAttribute("Id")
        id.Value = "0001"

        Dim title As XmlAttribute
        title = writer.CreateAttribute("Title")
        title.InnerText = "Bind Dropdown List"

        Dim visit As XmlAttribute
        visit = writer.CreateAttribute("Visit")
        visit.InnerText = "985"

        Dim modified As XmlAttribute
        modified = writer.CreateAttribute("Modified")
        modified.InnerText = "Jan 01, 2009"

        'Now save/write the XML file into server root location
        'Server.MapPath(".") is used to find the root path
        writer.Save(Server.MapPath(".") + "//XMLFile.xml")

        Response.Write("File Creadted: " + Server.MapPath(".") + "//XMLFile.xml")
    End Sub

Note: Don’t forget to add “Imports System.Xml” namespace.

Now if you check the application root directory you will found a XMLFile.xml file. Click here to view how we can bind this XML file into GridView. So now we can Programmatically create a XML file as well as we can use it in our application.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code