本文整理汇总了C#中System.Data.DataSet.WriteXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataSet.WriteXml方法的具体用法?C# DataSet.WriteXml怎么用?C# DataSet.WriteXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataSet
的用法示例。
在下文中一共展示了DataSet.WriteXml方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteXmlToFile
private void WriteXmlToFile(DataSet thisDataSet)
{
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "XmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream stream = new System.IO.FileStream
(filename, System.IO.FileMode.Create);
// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter xmlWriter =
new System.Xml.XmlTextWriter(stream,
System.Text.Encoding.Unicode);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(xmlWriter);
xmlWriter.Close();
}
示例2: WriteXmlToFile
private void WriteXmlToFile(DataSet thisDataSet)
{
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "XmlDoc.xml";
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(filename);
}
示例3: 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();
}
}
}
示例4: WriteXmlToFile
private void WriteXmlToFile(DataSet thisDataSet)
{
if (thisDataSet == null) { return; }
// Create a file name to write to.
string filename = "XmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream stream = new System.IO.FileStream
(filename, System.IO.FileMode.Create);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(stream);
}
示例5: Main
//引入命名空间
using System;
using System.Data;
using System.Data.SqlClient;
class WriteAndReadXML {
public static void Main() {
SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT TOP 2 CustomerID, CompanyName, ContactName, " +
"Address " +
"FROM Customers " +
"ORDER BY CustomerID";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
mySqlDataAdapter.Fill(myDataSet, "Customers");
mySqlConnection.Close();
myDataSet.WriteXml("myXmlFile.xml");
myDataSet.WriteXml("myXmlFile2.xml", XmlWriteMode.WriteSchema);
myDataSet.WriteXmlSchema("myXmlSchemaFile.xml");
myDataSet.Clear();
myDataSet.ReadXml("myXmlFile.xml");
DataTable myDataTable = myDataSet.Tables["Customers"];
foreach (DataRow myDataRow in myDataTable.Rows) {
Console.WriteLine("CustomerID = " + myDataRow["CustomerID"]);
Console.WriteLine("CompanyName = " + myDataRow["CompanyName"]);
Console.WriteLine("ContactName = " + myDataRow["ContactName"]);
Console.WriteLine("Address = " + myDataRow["Address"]);
}
}
}