Search - Articles
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
 

Writing an XML file using WriteXML method of DataSet and reading XML file with the help of ReadXML method of DataSet.

Author: Shahzad

XML has become a standard for the data storage and data transmission. In the .NET Framework XML is involved more with the technology that’s why better ways have been provided to deal with XML.

First of all get the path where your contact list file will be saved. I have decided to save this file in the bin directory of my application with Contacts.xml file name, you can decide your own location as required.

 

Dim XMLFilePath As String = Application.StartupPath & "\Contacts.xml"

Create DataSet and  DataTable  objects and also declare a DataRow object.

    Dim DS As New DataSet("Contacts")

    Dim DT As New DataTable("Contact")

    Dim DR As DataRow

 

Now, add some columns in the DataTable that you think will be used to save your contact list. For the sake of simplicity I have used only four columns.

 

 DT.Columns.Add("FirstName", System.Type.GetType("System.String"))

 DT.Columns.Add("LastName", System.Type.GetType("System.String"))

 DT.Columns.Add("EmailAddress", System.Type.GetType("System.String"))

 DT.Columns.Add("PhoneNumber", System.Type.GetType("System.String"))

Add this DataTable to DataSet.

DS.Tables.Add(DT)

 

Create a new DataRow from this DataTable

 

DR = DS.Tables("Contact").NewRow

 

Add the contact information in the DataRow. I have added just one contact’s information but you can make it more flexible by using your own programming logic.

 

    DR("FirstName") = "Contact First Name"

    DR("LastName") = "Contact Last Name"

    DR("EmailAddress") = "Contact Email Address"

    DR("PhoneNumber") = "Contact Phone Number"

 

Now, add this DataRow to DataTable.

 

     DT.Rows.Add(DR)

After you have added all the rows in the DataTable you will definitely need to write this information into an XML file. It’s very simple using DataSet. I’ll use WriteXML method of DataSet. It’s first parameter is the path where the file will be stored, and second parameter tells the method that the schema information is also needed to be written with the data in the xml file. Following statement will create an XML file at the specified location and will add the contact information in that file.

DS.WriteXml(XMLFilePath, XmlWriteMode.WriteSchema)

 

Whenever you need to read this information from the XML file you can use ReadXML method of DataSet as given below. First parameter of ReadXML method requires XML file path from where it’ll read the file, and second parameter tells the method that it needs to load the data by validating with the help of the schema information given in the XML file.

 

DS.ReadXml(XMLFilePath, XmlReadMode.ReadSchema)

 

Now, we have loaded all the data from the XML file into the DataSet.

 

We have to show this data to view the contact information. Obviously, this was the reason we had to store this information.

 

So, let’s take a simple approach and use a DataGrid to show this information.

 

I have used a DataGrid and named it as ContactsDataGrid

 

I simply need to set it’s DataSource property to the Table of the DataSet as given below.

 

Me.ContactsDataGrid.DataSource = DS.Tables("Contact")

 

Article Comments
Well. The code simple but helpful.
But i need more complex work to do. That is i want to save user controls which are define by me. And i am not able to do . Please can u help me for that.PLease.

Posted on 1/11/2007 4:06:29 AM by ruta karyakarte

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to get drive information of your physical media (Hard drive).

Disclaimer - Privacy
© 2002-2017 DevASP.net