Code
private void DemonstrateReadWriteXMLDocumentWithStreamReader()
{
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("dataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement= true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
OriginalDataSet.Tables.Add(table);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++)
{
newRow = table.NewRow();
newRow["item"]= "item " + i;
table.Rows.Add(newRow);
}
OriginalDataSet.AcceptChanges();
// Print out values of each table in the DataSet
// using the function defined below.
PrintValues(OriginalDataSet, "Original DataSet");
// Write the schema and data to an XML file.
string xmlFilename = "XmlDocument.xml";
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlFilename);
// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document into the DataSet.
newDataSet.ReadXml(xmlFilename);
// Print out values of each table in the DataSet
// using the function defined below.
PrintValues(newDataSet,"New DataSet");
}
private void PrintValues(DataSet dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable table in dataSet.Tables)
{
Console.WriteLine("TableName: " + table.TableName);
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.Write("\table " + row[column] );
}
Console.WriteLine();
}
}
}
The readxml method allows you to read only data or data and architecture from an XML document to dataset, while the readxmlschema method only reads the architecture. To read data and architecture at the same time, use one of the readxml reloads containing the mode parameter and set its value to readschema.
Note that the same applies to writexml and writexmlschema methods. To write XML data from dataset or schema and data, use the writexml method. To write only the schema, use the writexmlschema method.