SMUsamaShah
SMUsamaShah

Reputation: 7880

How to save to XML using dataset

I am using a sql database to save the data of a simple note taking application, using dataset and gui is binded with database. Simple work. Using SQL is useless here, I want to save the data to a simple XML file instead of SQL using the same dataset.

I am using Visual Studio 2010 and programming in C# .Net 4.0

Upvotes: 2

Views: 3560

Answers (4)

Jayesh Sorathia
Jayesh Sorathia

Reputation: 1614

Here are good example of generate XML from dataset

   DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    DataColumn dc;
    DataRow dr;
    ds.DataSetName = "products";
    dt.TableName = "product";

    dc = new DataColumn("product_id");
    dt.Columns.Add(dc);

    dc = new DataColumn("product_name");
    dt.Columns.Add(dc);

    dr = dt.NewRow();
    dr["product_id"] = 1;
    dr["product_name"] = "Monitor";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 2;
    dr["product_name"] = "Mouse";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 3;
    dr["product_name"] = "KeyBoard";
    dt.Rows.Add(dr);

    ds.Tables.Add(dt);
    string strXML= ds.GetXml();

    System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("datasetxml.xml"));
    sw.WriteLine(strXML);
    sw.Close();

Upvotes: 1

Baked Potato
Baked Potato

Reputation: 294

What i use is:

        private void buttonSaveXML_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFile = new SaveFileDialog();
        saveFile.Filter = "XML Files|*.xml";
        saveFile.Title = "Save a Xml File";
        saveFile.ShowDialog();
        if (saveFile.FileName != "")
        {
            FileStream fs =
                (FileStream)saveFile.OpenFile();
            DataSet.WriteXml(fs);
        }
    {

DataSet is the Dataset I use and It is a good idea to make a separate button to make it easy to use.

Upvotes: 2

Lorenzo
Lorenzo

Reputation: 29427

A dataset of a single table to XML

private void SingleTableToXml()
{
    DataSet myDS = getDataSet();

    // To write out the contents of the DataSet as XML,
    // use a file name to call the WriteXml method of the DataSet class
    myDS.WriteXml(Server.MapPath("filename.xml"), XmlWriteMode.IgnoreSchema);
}

if you have more than one table in the dataset, say it a master-detail relationship, then the method is exactly the same. Just make sure to create the DataRelation between tables and set the relation Nested property to true as in the following code

//Get the primary key column from the master table
DataColumn primarykey = myDS.Tables["Categories"].Columns["CategoryID"];
//Get the foreign key column from the detail table
DataColumn foreignkey = myDS.Tables["Products"].Columns["CategoryID"];

//Assign a relation
DataRelation relation = myDS.Relations.Add(primarykey, foreignkey);

//Ask ADO.NET to generate nested XML nodes
relation.Nested = true;

Hope it helps

Upvotes: 3

David
David

Reputation: 73554

You didn't specify your programming environment. Assuming you're using .NET....

Use the WriteXml method of the dataSet.

There's an article here: http://msdn.microsoft.com/en-us/library/ms233698%28VS.80%29.aspx

Upvotes: 1

Related Questions