Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Build the right apps the right way with powerful development tools.
Visual Studio 2010. Learn more...
Writing and Reading Data in a DataTable as XML in ASP.NET 2.0

Author: Aadil

In this article I’ll show you how you can use the DataTable object’s capabilities to read and write its contents as xml.

In the previous versions of ASP.NET the DataSet object was at our disposal for converting offline database data to xml format. You could use the DataSet object, populated with data from a single or more tables from a database and you could write that data in an xml file. Similarly you can read the same data from the disk file using the DataSet object’s methods.

 

Now in ASP.NET 2.0 most of this functionality is still present in the DataSet object but the DataTable object’s functionality has been raised too to include persisting its contents in an xml file and also for the reverse, reading the xml saved data back to construct the DataTable.

 

I’ll provide you with some sample code that reads a database table and fills that in the DataTable and then uses the DataTable object’s WriteXml method to save its contents to a file. The reverse process is also same, there is a method ReadXml that is similar and you can do that on your own. Here is the sample code.

 

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Dim objConn As New SqlConnection(ConfigurationManager.ConnectionStrings("db2ConnectionString").ConnectionString)

    objConn.Open()

   

    Dim dt As New DataTable

    Dim objCmd As New SqlCommand("select * from employees", objConn)

    Dim dr As SqlDataReader = objCmd.ExecuteReader()

    dt.Load(dr)

    dt.WriteXml("d:\writeDataTable.xml", False)

    dr.Close()

    dr = Nothing

    objCmd.Dispose()

    objCmd = Nothing

   

    objConn.Close()

    objConn = Nothing

  End Sub

Article Comments
This article is good. but when I try dt.writexml I am getting the message datatable not serialized.

Posted on 2/7/2008 2:53:21 PM by Selvaraj

good post, helpful for the learner.

Posted on 6/6/2010 4:39:54 AM by Mohammad

datatable declare in classfile than how to write data from datatable into xml i got error:"data table name is not set"

Posted on 7/14/2010 2:26:36 AM by parimala

I want to store data in xml file from a asp.net web page like i have two text box ID, NAME that i want store id and name in xml file plz rly

Posted on 8/20/2010 9:01:01 AM by Arvind Kumar

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using DataTable instead of DataSet in ASP.NET 2.0

Disclaimer - Privacy
© 2002-2010 DevASP.net