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);
        writer.AppendChild(declaration);

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

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

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

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

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

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

        // 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)
        writer.AppendChild(declaration)

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

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

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

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

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

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

        '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

Posted in .Net, Asp.net, C#, Gridview, VB.Net, XML

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*