Wednesday, January 9, 2013

Export DataSet to XML File

Export DataSet to XML File

: create xml file from a dataset
This code snippet show how to export DataSet to xml file.Here we have taken data from database and fill dataset.We have taken a aspx page and a button there,after clicking the button a xml file will be created at specified location.

 public void CreateXml()  
   {  
     DataSet ds = new DataSet();  
     SqlDataAdapter da = new SqlDataAdapter("usp_GetAddressDetails",con);  
     da.Fill(ds);  
     if (ds.Tables[0].Rows.Count > 0)  
     {  
       GridView1.DataSource = ds;  
       GridView1.DataBind();  
     }  
     StreamWriter objStreamWriter = new StreamWriter("E:\\\\ASP\\XMLFiles

      \\EmpAddress.xml", false);  

     ds.WriteXml(objStreamWriter);  
     objStreamWriter.Close();  
   }  
   protected void Button1_Click(object sender, EventArgs e)  
   {  
     CreateXml();  
   }  


0 comments:

Post a Comment