Now before creating the application, let us create a table named employee in a database from where we show the records in a Grid view, the table has the following fields (shown in the following image):
 
 
I hope you have created the same type of table.
     - "Start" - "All Programs" - "Microsoft Visual Studio 2010".
     
- "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
     
- Provide the Project name such as ExportGridRecordsToXML or another as you wish and specify the location.
     
- Then right-click on Solution Explorer - "Add New Item" - Default.aspx page.
     
- One Button and a grid view.
Now the default.aspx source code will be such as follows:
     - <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ExPortGridviewToXML.Default" %>  
     
-   
     
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
     
- <html>  
     
- <head id="Head1" runat="server">  
     
-     <title></title>  
     
- </head>  
     
- <body bgcolor="Silver">  
     
-     <form id="form1" runat="server">  
     
-     <br />  
     
-     <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">  
     
-         Article by Vithal Wadje</h2>  
     
-     <br />  
     
-     <div>  
     
-         <asp:GridView ID="GridView1" runat="server" CellPadding="6" ForeColor="#333333" GridLines="None">  
     
-             <AlternatingRowStyle BackColor="White" />  
     
-             <EditRowStyle BackColor="#7C6F57" />  
     
-             <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
     
-             <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />  
     
-             <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />  
     
-             <RowStyle BackColor="#E3EAEB" />  
     
-             <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />  
     
-             <SortedAscendingCellStyle BackColor="#F8FAFA" />  
     
-             <SortedAscendingHeaderStyle BackColor="#246B61" />  
     
-             <SortedDescendingCellStyle BackColor="#D4DFE1" />  
     
-             <SortedDescendingHeaderStyle BackColor="#15524A" />  
     
-         </asp:GridView>  
     
-         <br />  
     
-                    <asp:Button ID="Button1" runat="server"  
     
-             Text="Export" OnClick="Button1_Click" />  
     
-     </div>  
     
-     </form>  
     
- </body>  
     
- </html>  
 
 
 
Now let us create a function to bind the records to the Grid view from the database. If you are a beginner and don't understand in detail how to bind a Grid view from a database then refer to my following article.
Now, for this article create the following function in the default.aspx.cs page to bind the Grid view:
     - private void Bindgrid()    
- {    
-     connection();    
-     query = "select *from Employee";  
-     com = new SqlCommand(query, con);    
-     SqlDataAdapter da = new SqlDataAdapter(query, con);    
-     DataSet ds = new DataSet();    
-        
-     da.Fill(ds);    
-     DataTable GridSource = ds.Tables[0];    
-     GridView1.DataSource = GridSource;    
-     GridView1.DataBind();    
-     con.Close();    
-     ViewState["Data"] = GridSource;    
- } 
 
 
Now, call the preceding function on page load as:
     - protected void Page_Load(object sender, EventArgs e)    
- {    
-     if (!IsPostBack)    
-     {    
-         Bindgrid();    
-   
-     }    
- } 
 
 
Now create the following function to export the Grid view records to XML:
     - private void ExportGridToXML()    
- {  
-     SaveFileDialog SaveXMLFileDialog = new SaveFileDialog();    
-     SaveXMLFileDialog.Filter = "Xml files (*.xml)|*.xml";    
-     SaveXMLFileDialog.FilterIndex = 2;    
-     SaveXMLFileDialog.RestoreDirectory = true;    
-     SaveXMLFileDialog.InitialDirectory = "C:\\";    
-     SaveXMLFileDialog.FileName = "Vithal_Wadje";    
-     SaveXMLFileDialog.Title = "XML Export";    
-     if (SaveXMLFileDialog.ShowDialog() == DialogResult.OK)    
-     {        
-         DataSet ds = new DataSet();    
-         DataTable dtxml = (DataTable)ViewState["Data"];    
-         ds.Tables.Add(dtxml);    
-         ds.WriteXml(File.OpenWrite(SaveXMLFileDialog.FileName));           
-     }    
-     XMLFileThread.Abort();        
- } 
 
 
We have created the preceding function that is used to export the dataTable to XML that is used for a GridView as a DataSource.
 
Now call the preceding function on the Export button click as: 
     - protected void Button1_Click(object sender, EventArgs e)    
- {    
-      XMLFileThread = new Thread(new ThreadStart(ExportGridToXML));    
-     XMLFileThread.ApartmentState = ApartmentState.STA;    
-     XMLFileThread.Start();  
- } 
 
 
The entire code of the default.aspx page will look such as follows:
     - using System;  
     
- using System.IO;  
     
- using System.Windows.Forms;  
     
- using System.Data;  
     
- using System.Threading;  
     
- using System.Data.SqlClient;  
     
- using System.Configuration;  
     
-   
     
- namespace ExPortGridviewToXML  
     
- {  
     
-     public partial class Default : System.Web.UI.Page  
     
-     {  
     
-         private SqlConnection con;  
     
-         private SqlCommand com;  
     
-         private string constr, query;  
     
-         Thread XMLFileThread;  
     
-         private void connection()  
     
-         {  
     
-             constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();  
     
-             con = new SqlConnection(constr);  
     
-             con.Open();  
     
-   
     
-         }  
     
-         protected void Page_Load(object sender, EventArgs e)  
     
-         {  
     
-             if (!IsPostBack)  
     
-             {  
     
-                 Bindgrid();  
     
-   
     
-             }  
     
-         }
-         private void Bindgrid()  
     
-         {  
     
-             connection();  
     
-             query = "select *from Employee";  
     
-             com = new SqlCommand(query, con);  
     
-             SqlDataAdapter da = new SqlDataAdapter(query, con);  
     
-             DataSet ds = new DataSet();  
     
-              
     
-             da.Fill(ds);  
     
-             DataTable GridSource = ds.Tables[0];  
     
-             GridView1.DataSource = GridSource;  
     
-             GridView1.DataBind();  
     
-             con.Close();  
     
-             ViewState["Data"] = GridSource;  
     
-         }  
     
-         protected void Button1_Click(object sender, EventArgs e)  
     
-         {  
     
-              XMLFileThread = new Thread(new ThreadStart(ExportGridToXML));  
     
-             XMLFileThread.ApartmentState = ApartmentState.STA;  
     
-             XMLFileThread.Start();
-         }  
     
-         private void ExportGridToXML()  
     
-         {  
     
-   
     
-             SaveFileDialog SaveXMLFileDialog = new SaveFileDialog();  
     
-             SaveXMLFileDialog.Filter = "Xml files (*.xml)|*.xml";  
     
-             SaveXMLFileDialog.FilterIndex = 2;  
     
-             SaveXMLFileDialog.RestoreDirectory = true;  
     
-             SaveXMLFileDialog.InitialDirectory = "C:\\";  
     
-             SaveXMLFileDialog.FileName = "Vithal_Wadje";  
     
-             SaveXMLFileDialog.Title = "XML Export";  
     
-             if (SaveXMLFileDialog.ShowDialog() == DialogResult.OK)  
     
-             {  
     
-               
     
-                 DataSet ds = new DataSet();  
     
-                 DataTable dtxml = (DataTable)ViewState["Data"];  
     
-                 ds.Tables.Add(dtxml);  
     
-                 ds.WriteXml(File.OpenWrite(SaveXMLFileDialog.FileName));  
     
-                  
     
-             }  
     
-             XMLFileThread.Abort();  
     
-               
     
-         }
-     }  
     
- }  
 
 
 
Now run the application and then we can see the following records in the Grid view:
 
 
Now click on the export button. The XML file that is created will be saved on my Desktop because I have given the C drive path in the code, however you can specify any path as you wish, the file will look such as follows:
 
 
Now open the file using Visual Studio or any browser. I will open it with the IE browser then the records will look such as follows:
 
Now you see that all the records of the Grid view are exported to an XML file.
Notes
     -  Download the Zip file from the attachment for the full source code of the application.
-  Change the connection string in the web.config file to specify your server location.
-  You can save the XML file that is created to any location.
-  We are actually exporting the Data Table to XML that is used for a Grid view as a Data Source.
Summary
I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.