本文整理汇总了C#中System.Data.DataSet.ReadXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataSet.ReadXml方法的具体用法?C# DataSet.ReadXml怎么用?C# DataSet.ReadXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataSet
的用法示例。
在下文中一共展示了DataSet.ReadXml方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DemonstrateReadWriteXMLDocumentWithStreamReader
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();
}
}
}
示例2: DemonstrateReadWriteXMLDocumentWithFileStream
private void DemonstrateReadWriteXMLDocumentWithFileStream()
{
// Create a DataSet with one table and two columns.
DataSet originalDataSet = new DataSet("dataSet");
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 XML file with FileStream.
string xmlFilename = "XmlDocument.xml";
System.IO.FileStream streamWrite = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Use WriteXml to write the XML document.
originalDataSet.WriteXml(streamWrite);
// Close the FileStream.
streamWrite.Close();
// Dispose of the original DataSet.
originalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream streamRead = new System.IO.FileStream
(xmlFilename,System.IO.FileMode.Open);
newDataSet.ReadXml(streamRead);
// 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();
}
}
}
示例3: DemonstrateReadWriteXMLDocumentWithXMLReader
private void DemonstrateReadWriteXMLDocumentWithXMLReader()
{
// 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 XML schema and data to file with FileStream.
string xmlFilename = "XmlDocument.xml";
// Create FileStream
System.IO.FileStream fsWriteXml =
new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Create an XmlTextWriter to write the file.
System.Xml.XmlTextWriter xmlWriter =
new System.Xml.XmlTextWriter
(fsWriteXml, System.Text.Encoding.Unicode);
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter, XmlWriteMode.WriteSchema);
// Close the FileStream.
fsWriteXml.Close();
// Dispose of the original DataSet.
OriginalDataSet.Dispose();
// Create a new DataSet.
DataSet newDataSet = new DataSet("New DataSet");
// Read the XML document back in.
// Create new FileStream to read schema with.
System.IO.FileStream fsReadXml =
new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Open);
// Create an XmlTextReader to read the file.
System.Xml.XmlTextReader xmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema);
// Close the XmlTextReader
xmlReader.Close();
// Print out values of each table in the DataSet
// fusing the unction 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("\t{0}({1})='{2}'", column.ColumnName, column.DataType.Name, row[column]);
}
Console.WriteLine();
}
}
}
示例4: Main
//引入命名空间
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
DataSet thisDataSet = new DataSet();
thisDataSet.ReadXml("nwinddata.xml");
foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) {
Console.WriteLine("Customer ID: " + custRow["CustomerID"] +
" Name: " + custRow["CompanyName"]);
}
Console.WriteLine("Table created by ReadXml is called {0}",
thisDataSet.Tables[0].TableName);
}
}