Home > DeveloperSection > Forums > Create XML file from Dataset in C#
Goti Bandu

Total Post:119

Points:835
Posted on    June-15-2013 5:08 AM

 C# C# 
Ratings:


 1 Reply(s)
 812  View(s)
Rate this:
Hi Developers,

How can I create a XML file from Dataset in C#?



Vijay Shukla

Total Post:100

Points:700
Posted on    July-08-2013 2:00 AM

Hi Goti,
You can use this code:

using System;

using System.Data;

using System.Windows.Forms;

using System.Xml;

 

 

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        DataTable dt;

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            DataSet ds = new DataSet();

            dt = new DataTable();

            dt.Columns.Add(new DataColumn("Product_ID"Type.GetType("System.Int32")));

            dt.Columns.Add(new DataColumn("Product_Name"Type.GetType("System.String")));

            dt.Columns.Add(new DataColumn("product_Price"Type.GetType("System.Int32")));

            fillRows(1, "product1", 1111);

            fillRows(2, "product2", 2222);

            fillRows(3, "product3", 3333);

            fillRows(4, "product4", 4444);

            ds.Tables.Add(dt);

            ds.Tables[0].TableName = "product";

            ds.WriteXml("Product.xml");

            MessageBox.Show("Done");

        }

 

        private void fillRows(int pID, string pName, int pPrice)

        {

            DataRow dr;

            dr = dt.NewRow();

            dr["Product_ID"] = pID;

            dr["Product_Name"] = pName;

            dr["product_Price"] = pPrice;

            dt.Rows.Add(dr);

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick