Convert Dataset Datatable or GridView to XML in ASP.Net using C#.Net or VB.Net How to use WriteXml() in C#.Net or VB.Net

In this tutorial i am going to explain about how to convert dataset datatable or gridview into xml document in Asp.Net using C#.Net & VB.Net or How to use WriteXml() in C#.Net or VB.Net. It is achieved using DataTable.WriteXml() method in the System.Data namespace.
I have created a new empty website and added a webform to the website. And i have added a gridview to the webform and added a button. The button onclick event is linked with btnConvert_Click event handler. Below is the html markup used.

HTML Markup:

In the code behind file on page load event i have created a datatable and inserted some sample records in that datatable and binded it to gridview and also i added it to viewstate. On the button click event i have read the datatable from viewstate and converted it xml using DataTable.WriteXml() method in the System.Data namespace. This methos convert the datatable into xml and stores it to the xml file mentioned as the parameter. Below is the code used.

C#.Net:

VB.Net:

Output of how to convert dataset datatable or gridview into xml document in Asp.Net using C#.Net & VB.Net or How to use WriteXml() in C#.Net or VB.Net:

Output of how to convert dataset datatable or gridview into xml document in Asp.Net using C#.Net & VB.Net or How to use WriteXml() in C#.Net or VB.Net:

SourceCode of how to convert dataset datatable or gridview into xml document in Asp.Net using C#.Net & VB.Net or How to use WriteXml() in C#.Net or VB.Net:

SourceCode of how to convert dataset datatable or gridview into xml document in Asp.Net using C#.Net & VB.Net or How to use WriteXml() in C#.Net or VB.Net:

You May Also Like…

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!