Create XML file from a DataTable in Asp.Net C# VB.Net

XML file is necessary in many cases. Usually we make XML file from Database. But we can make XML file from DataTbale also. Here in this article i will explain how you can make or create an XML file from DataTable.

Create a DataTable First under Page_Load Event:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable oTable = new DataTable("Article");

            //Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
            oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));
            oTable.Columns.Add(new DataColumn("Published", typeof(System.DateTime)));

            //Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now);
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now.AddDays(-1));
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now.AddDays(-2));
            oTable.Rows.Add(1004, "Data Table Sorting", DateTime.Now);
            oTable.Rows.Add(1004, "Data View Sorting", DateTime.Now.AddDays(-5));
        }
    }

Note: Don’t forget to include “System.Data” namespace.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oTable As DataTable = New DataTable("Article")

            'Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(New DataColumn("ID", System.Type.GetType("System.Int64")))
            oTable.Columns.Add(New DataColumn("Title", System.Type.GetType("System.String")))
            oTable.Columns.Add(New DataColumn("Published", System.Type.GetType("System.DateTime")))

            'Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now)
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now.AddDays(-1))
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now.AddDays(-2))
            oTable.Rows.Add(1004, "Data Table Sorting", DateTime.Now)
            oTable.Rows.Add(1004, "Data View Sorting", DateTime.Now.AddDays(-5))

        End If
    End Sub

Now Create XML file From DataTable:
It’s just one line code. DataTable provides us WriteXml method to convert whole set of data into a XML file.
C# Code:

oTable.WriteXml(Server.MapPath(".") + "/XMLFile.xml");

Note: Check your root folder to get the “XMLFile.xml” XML file.

VB.Net Code:

oTable.WriteXml(Server.MapPath(".") + "/XMLFile.xml")

Note: Check your root folder to get the “XMLFile.xml” XML file.

The XML File:
XML_File

Download Code Example C#        Download Code Example VB.Net

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

Leave a Reply

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

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*