一样: 这是读取的方法: private void DemonstrateReadWriteXMLDocumentWithXMLReader(){ // Create a DataSet with one table and two columns. DataSet OriginalDataSet = new DataSet("myDataSet"); OriginalDataSet.Namespace= "NetFrameWork"; DataTable myTable = new DataTable("myTable"); DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32")); c1.AutoIncrement= true; DataColumn c2 = new DataColumn("item"); myTable.Columns.Add(c1); myTable.Columns.Add(c2); OriginalDataSet.Tables.Add(myTable); // Add ten rows. DataRow newRow; for(int i = 0; i < 10; i++){ newRow = myTable.NewRow(); newRow["item"]= "item " + i; myTable.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 = "myXmlDocument.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); // 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 myXmlReader = new System.Xml.XmlTextReader(fsReadXml); // Read the XML document into the DataSet. newDataSet.ReadXml(myXmlReader); // Close the XmlTextReader myXmlReader.Close(); // Print out values of each table in the DataSet using the // function defined below. PrintValues(newDataSet,"New DataSet"); }private void PrintValues(DataSet ds, string label){ Console.WriteLine("\n" + label); foreach(DataTable t in ds.Tables){ Console.WriteLine("TableName: " + t.TableName); foreach(DataRow r in t.Rows){ foreach(DataColumn c in t.Columns){ Console.Write("\t " + r[c] ); } Console.WriteLine(); } } } 读取以后用DataSet的Merge方法就会把数据合并起来 3..然后再写进XML 以下示例创建 System.IO.FileStream 对象,该对象用于创建新的 System.Xml.XmlTextWriter。XmlTextWriter 对象和 WriteXml 方法一起用于写 XML 文档。[Visual Basic, C#] 注意 此示例显示如何使用 WriteXml 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。 [Visual Basic] Private Sub WriteXmlToFile(thisDataSet As DataSet) If thisDataSet Is Nothing Then Return End If ' Create a file name to write to. Dim filename As String = "myXmlDoc.xml" ' Create the FileStream to write with. Dim myFileStream As New System.IO.FileStream _ (filename, System.IO.FileMode.Create) ' Create an XmlTextWriter with the fileStream. Dim myXmlWriter As New System.Xml.XmlTextWriter _ (myFileStream, System.Text.Encoding.Unicode) ' Write to the file with the WriteXml method. thisDataSet.WriteXml(myXmlWriter) myXmlWriter.Close() End Sub [C#] private void WriteXmlToFile(DataSet thisDataSet) { if (thisDataSet == null) { return; } // Create a file name to write to. string filename = "myXmlDoc.xml"; // Create the FileStream to write with. System.IO.FileStream myFileStream = new System.IO.FileStream (filename, System.IO.FileMode.Create); // Create an XmlTextWriter with the fileStream. System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode); // Write to the file with the WriteXml method. thisDataSet.WriteXml(myXmlWriter); myXmlWriter.Close(); }
这是读取的方法:
private void DemonstrateReadWriteXMLDocumentWithXMLReader(){
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable myTable = new DataTable("myTable");
DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement= true;
DataColumn c2 = new DataColumn("item");
myTable.Columns.Add(c1);
myTable.Columns.Add(c2);
OriginalDataSet.Tables.Add(myTable);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++){
newRow = myTable.NewRow();
newRow["item"]= "item " + i;
myTable.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 = "myXmlDocument.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);
// 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 myXmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader);
// Close the XmlTextReader
myXmlReader.Close(); // Print out values of each table in the DataSet using the
// function defined below.
PrintValues(newDataSet,"New DataSet");
}private void PrintValues(DataSet ds, string label){
Console.WriteLine("\n" + label);
foreach(DataTable t in ds.Tables){
Console.WriteLine("TableName: " + t.TableName);
foreach(DataRow r in t.Rows){
foreach(DataColumn c in t.Columns){
Console.Write("\t " + r[c] );
}
Console.WriteLine();
}
}
}
读取以后用DataSet的Merge方法就会把数据合并起来
3..然后再写进XML
以下示例创建 System.IO.FileStream 对象,该对象用于创建新的 System.Xml.XmlTextWriter。XmlTextWriter 对象和 WriteXml 方法一起用于写 XML 文档。[Visual Basic, C#] 注意 此示例显示如何使用 WriteXml 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Private Sub WriteXmlToFile(thisDataSet As DataSet)
If thisDataSet Is Nothing Then
Return
End If
' Create a file name to write to.
Dim filename As String = "myXmlDoc.xml"
' Create the FileStream to write with.
Dim myFileStream As New System.IO.FileStream _
(filename, System.IO.FileMode.Create)
' Create an XmlTextWriter with the fileStream.
Dim myXmlWriter As New System.Xml.XmlTextWriter _
(myFileStream, System.Text.Encoding.Unicode)
' Write to the file with the WriteXml method.
thisDataSet.WriteXml(myXmlWriter)
myXmlWriter.Close()
End Sub
[C#]
private void WriteXmlToFile(DataSet thisDataSet) {
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "myXmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream myFileStream = new System.IO.FileStream
(filename, System.IO.FileMode.Create);
// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter myXmlWriter =
new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(myXmlWriter);
myXmlWriter.Close();
}