How to write Gridview data to XML file in .NET

No.of Views952
Bookmarked0 times
Downloads 
Votes0
By  youngmurukan   On  29 Dec 2010 21:12:45
Tag : CSharp , Grid Controls
In this snippet I will show how to convert DataSet to XML in .NET. The DataSet is flexible .NET Objects to convert Xml with WriteXml() method.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this snippet I will show how to convert DataSet to XML. The DataSet is flexible .NET Objects to convert Xml with WriteXml() method.

C# Code

DataSet ds = new DataSet("TestDataset");
DataTable dt = new DataTable("TestDataTable");
dt.Columns.Add("Id", System.Type.GetType("System.String"));
ds.Tables.Add(dt);
//Add any number columns here
for (int count = 0; count < GridView1.Rows.Count; count++)
{
DataRow dr = dt.NewRow();
dr[0] = GridView1.Rows[count].Cells[0].Text;
//you can add values to all columns in datatable
dt.Rows.Add(dr);
}
ds.Tables[0].WriteXml("d:\\test2.xml"); 

VB.NET

Dim ds As New DataSet("TestDataset")
Dim dt As New DataTable("TestDataTable")
dt.Columns.Add("Id", System.Type.[GetType]("System.String"))
ds.Tables.Add(dt)
'Add any number columns here
For count As Integer = 0 To GridView1.Rows.Count - 1
	Dim dr As DataRow = dt.NewRow()
	dr(0) = GridView1.Rows(count).Cells(0).Text
	'you can add values to all columns in datatable
	dt.Rows.Add(dr)
Next
ds.Tables(0).WriteXml("d:\test2.xml")

 hopes help.

 
Sign Up to vote for this article
 
About Author
 
youngmurukan
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-12 May 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top