ARTICLE

Methods for Transforming data to a XML file

Posted by Lalitha Venkatasubramanian Articles | ADO.NET in VB.NET August 08, 2006
This articles mentions different ways to tranform data to XML file.
Download Files:
 
Reader Level:

Here are the different methods to transform a data file to a XML Document:

 

Method1:

 

The first idea of converting a piece of data which are in a text files to a Xml Document is by using a DataSet. By using a DataSet, we can bind that data to the DataGrid and do the updation, deletion and addition operations. Even though the final output is the same Xml file, we have the Dataset in between to manipulate the data.

 

Example:

 

Imports System

Imports System.Collections

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Web

Imports System.Web.SessionState

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.HtmlControls

Imports System.Xml

Imports System.IO

 

    

Public Class WebForm1

    Inherits System.Web.UI.Page

    Dim dataSet As New dataSet("dataSet")

    Dim dt As New DataTable

    Dim dr As DataRow

#Region " Web Form Designer Generated Code "

 

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

 

    End Sub

    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    Protected WithEvents Label2 As System.Web.UI.WebControls.Label

    Protected WithEvents txtName As System.Web.UI.WebControls.TextBox

    Protected WithEvents txtPlace As System.Web.UI.WebControls.TextBox

    Protected WithEvents Fill As System.Web.UI.WebControls.Button

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

 

    'NOTE: The following placeholder declaration is required by the Web Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

 

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN: This method call is required by the Web Form Designer

        'Do not modify it using the code editor.

        InitializeComponent()

    End Sub

 

#End Region

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Put user code to initialize the page here

        If Not IsPostBack Then

            Try

                dataSet.ReadXml(Server.MapPath("sample.xml"))

            Catch ex As Exception

                If dataSet.Tables.Count = 0 Then

                    dataSet.WriteXml(Server.MapPath("sample.xml"))

                End If

                Response.Write(ex.Message)

            End Try

        End If

 

    End Sub

 

    Private Sub Fill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fill.Click

        dataSet.ReadXml(Server.MapPath("sample.xml"))

        If dataSet.Tables.Count = 0 Then

            Dim dc1 As New DataColumn("Name")

            Dim dc2 As New DataColumn("Place")

            dataSet.Tables.Add(dt)

            dataSet.Tables(0).Columns.Add(dc1)

            dataSet.Tables(0).Columns.Add(dc2)

        End If

        dr = dataSet.Tables(0).NewRow()

        dr("Name") = txtName.Text

        dr("Place") = txtPlace.Text

        dataSet.Tables(0).Rows.Add(dr)

        dataSet.AcceptChanges()

        dataSet.WriteXml(Server.MapPath("sample.xml"))

        BindData()

    End Sub

    Private Sub BindData()

        DataGrid1.DataSource = dataSet

        DataGrid1.DataBind()

    End Sub 'BindData

 

End Class

 

Here in this, on the load method, we are reading the xml file which is created using a DataSet. If it is not getting created it is been catched and it is created. When the Fill Button in the form is clicked after entering the values - Name and Place, a new row is been created and a xml file is generated using the DataSet.WriteXml method. The DataTable, DataColumn, DataRow are used for this purposes.

 

Then for binding the DataGrid, we use the DataGird.DataSource =the dataset.

 

Method2:

 

The second method is to use the XmlDocument. This class uses the xmlTextWriter and using that the xmlfile is obtained.

 

Example:

 

Imports System

Imports System.Collections

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Web

Imports System.Web.SessionState

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.HtmlControls

Imports System.Xml

 

   

Public Class WebForm1

    Inherits System.Web.UI.Page

 

#Region " Web Form Designer Generated Code "

 

    'This call is required by the Web Form Designer.

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

 

    End Sub

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    Protected WithEvents Fill As System.Web.UI.WebControls.Button

    Protected WithEvents txtPlace As System.Web.UI.WebControls.TextBox

    Protected WithEvents Label2 As System.Web.UI.WebControls.Label

    Protected WithEvents txtName As System.Web.UI.WebControls.TextBox

    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable

 

    'NOTE: The following placeholder declaration is required by the Web Form Designer.

    'Do not delete or move it.

    Private designerPlaceholderDeclaration As System.Object

 

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        'CODEGEN: This method call is required by the Web Form Designer

        'Do not modify it using the code editor.

        InitializeComponent()

    End Sub

 

#End Region

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then

            'Put user code to initialize the page here

        End If

    End Sub

 

    Private Sub Fill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fill.Click

        Try

            Dim filename As String = Server.MapPath("sample2.xml")

            Dim xmlDoc As New XmlDocument

            Try

                xmlDoc.Load(filename)

            Catch

            End Try

            Dim root As XmlNode = xmlDoc.DocumentElement

            Dim childNode As XmlElement = xmlDoc.CreateElement("Employee")

            root.AppendChild(childNode)

            Dim childNode2 As XmlElement = xmlDoc.CreateElement("Name")

            Dim textNode1 As XmlText = xmlDoc.CreateTextNode(txtName.Text)

            childNode2.AppendChild(textNode1)

            childNode.AppendChild(childNode2)

            Dim childNode3 As XmlElement = xmlDoc.CreateElement("Place")

            Dim textNode2 As XmlText = xmlDoc.CreateTextNode(txtPlace.Text)

            childNode3.AppendChild(textNode2)

            childNode.AppendChild(childNode3)

            xmlDoc.Save(filename)

        Catch ex As Exception

            Response.Write(ex.ToString())

        End Try

 

    End Sub

End Class

The main point to be noted here is that we are not using any dataset here. Both the two methods use and produce the same xml file as output. But the difference is that, we cannot bind it to a DataGrid as we are not using any DataSets to be indulged in this. There are many other methods also ie., by using XMLDataDocument also.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM). 

share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor