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
ESET
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Life runs on code. Find out how Visual Studio 2010 can help you realize your vision. 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

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

Disclaimer - Privacy
© 2002-2010 DevASP.net