---
title: "Insert data from XML to database in C#"  
description: "Insert data from XML to database in C#"  
author: "Anonymous User"  
published: 2013-06-15  
updated: 2013-06-15  
canonical: https://www.mindstick.com/forum/1178/insert-data-from-xml-to-database-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Insert data from XML to database 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 [insert data](https://www.mindstick.com/forum/33964/insert-data-in-datagridview-from-text-box-c-sharp) from [XML](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) to [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax)?

Thanks in advance.

## Replies

### Reply by Sumit Kesarwani

Hi Pravesh,\
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;            SqlCommand command;            SqlDataAdapter adpter = new SqlDataAdapter();            DataSet ds = new DataSet();            XmlReader xmlFile;            string sql = null;             int product_ID = 0;            string Product_Name = null;            double product_Price = 0;             connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";             connection = new SqlConnection(connetionString);             xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());            ds.ReadXml(xmlFile);            int i = 0;            connection.Open();            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)            {                product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);                Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();                product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);                sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";                command = new SqlCommand(sql, connection);                adpter.InsertCommand = command;                adpter.InsertCommand.ExecuteNonQuery();            }            connection.Close();            MessageBox.Show("Done .. ");        }    }}
```


---

Original Source: https://www.mindstick.com/forum/1178/insert-data-from-xml-to-database-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
