DataSet ReadXml ReadXmlSchema (from msdn)

代码
   
     
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();
}
}
}

 

ReadXml 方法提供了只将数据或同时将数据和架构从 XML 文档读入 DataSet 的方式,而 ReadXmlSchema 方法仅读架构。若要同时读数据和架构,请使用包括 mode 参数的 ReadXML 重载之一,并将其值设置为 ReadSchema。

请注意,对于 WriteXml 和 WriteXmlSchema 方法也是如此。若要写入来自 DataSet 的 XML 数据或架构和数据两者,使用 WriteXml 方法。若要只写入架构,请使用 WriteXmlSchema 方法。

你可能感兴趣的:(schema)