---
title: "Create an XML file from SQL in C#"  
description: "Create an XML file from SQL in C#"  
author: "Anonymous User"  
published: 2013-06-15  
updated: 2013-06-15  
canonical: https://www.mindstick.com/forum/1174/create-an-xml-file-from-sql-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Create an XML file from SQL in C#

Hi [Developers](https://www.mindstick.com/articles/12875/blunders-you-must-avoid-while-hiring-java-developers-to-get-the-best-fulfilling-your-needs),\

How to create [XML file](https://www.mindstick.com/forum/360/how-to-read-xml-file-in-php) [form](https://www.mindstick.com/forum/6/multi-form-mfc-application) [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) in C#?

Thanks in advance.

## Replies

### Reply by Sumit Kesarwani

Hi John,\
You can use this code:

```
using System;using System.Data;using System.Windows.Forms;using System.Xml;using System.Data.SqlClient; namespace WindowsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }         private void button1_Click(object sender, EventArgs e)        {            string connetionString = null;            SqlConnection connection );            SqlDataAdapter adapter ;            DataSet ds = new DataSet();            string sql = null;             connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";            connection = new SqlConnection(connetionString);            sql = "select * from Product";            try            {                connection.Open();                adapter = new SqlDataAdapter(sql, connection);                adapter.Fill(ds);                connection.Close();                ds.WriteXml("Product.xml");                MessageBox.Show("Done");            }            catch (Exception ex)            {                MessageBox.Show (ex.ToString());             }        }    }}
```


---

Original Source: https://www.mindstick.com/forum/1174/create-an-xml-file-from-sql-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
