Save data in a DataSet to xml document : Xml DataSet « XML « C# / CSharp Tutorial






using System;
using System.IO;
using System.Xml;
using System.Data;

class MainClass
{
  public static void Main()
  {
    DataSet ds = new DataSet("DS");
    ds.Namespace = "StdNamespace";
    DataTable stdTable = new DataTable("Student");
    DataColumn col1 = new DataColumn("Name");
    DataColumn col2 = new DataColumn("Address");
    stdTable.Columns.Add(col1);
    stdTable.Columns.Add(col2);
    ds.Tables.Add(stdTable);

    DataRow newRow;newRow = stdTable.NewRow();
    newRow["Name"]= "M C";
    newRow["Address"]= "address 1";
    stdTable.Rows.Add(newRow);
    newRow = stdTable.NewRow();
    newRow["Name"]= "M G";
    newRow["Address"]= "address1";
    stdTable.Rows.Add(newRow);
    ds.AcceptChanges();

    StreamWriter myStreamWriter = new StreamWriter(@"c:\stdData.xml");

    ds.WriteXml(myStreamWriter);
    myStreamWriter.Close();
  }
}








30.28.Xml DataSet
30.28.1.Fill DataSet with the data from XML
30.28.2.Load XML to DataSet
30.28.3.Save data in a DataSet to xml document
30.28.4.Create Data with DataSet and DataRow and save to xml
30.28.5.Output DataSet to Xml
30.28.6.Reading XML with DataSet