Private voidDemonstratereadwritexmldocumentwithstreamreader () {//Create a DataSet with one table and both columns.DataSet Originaldataset =NewDataSet ("DataSet"); Originaldataset.namespace="NETFramework"; DataTable Table=NewDataTable ("Table"); DataColumn Idcolumn=NewDataColumn ("ID", Type.GetType ("System.Int32")); Idcolumn.autoincrement=true; DataColumn Itemcolumn=NewDataColumn ("Item"); Table. Columns.Add (Idcolumn); Table. Columns.Add (Itemcolumn); ORIGINALDATASET.TABLES.ADD (table); //Add ten rows.DataRow NewRow; for(inti =0; I <Ten; i++) {NewRow=table. NewRow (); newrow["Item"]="Item"+i; Table. Rows.Add (NewRow); } originaldataset.acceptchanges (); //Print out values of all table in the DataSet//using the function defined below.Printvalues (Originaldataset,"Original DataSet"); //Write the schema and data to an XML file. stringXMLfileName ="Xmldocument.xml"; //Use WriteXml to write the document.Originaldataset.writexml (XMLfileName); //Dispose of the original DataSet.Originaldataset.dispose (); //Create a new DataSet.DataSet NewDataSet =NewDataSet ("New DataSet"); //Read the XML document into the DataSet.Newdataset.readxml (XMLfileName); //Print out values of all table in the DataSet//using the function defined below.Printvalues (NewDataSet,"New DataSet");}Private voidPrintvalues (DataSet DataSet,stringlabel) {Console.WriteLine ("\ n"+label); foreach(DataTable tableinchdataset.tables) {Console.WriteLine ("TableName:"+table. TableName); foreach(DataRow rowinchtable. Rows) {foreach(DataColumn columninchtable. Columns) {Console.Write ("\table"+Row[column]); } Console.WriteLine (); } }}
The operation of the dataset on XML. WriteXml () and ReadXml. DataSet. AcceptChanges (). DataSet. DIspose (). Freeing resources