DataSet.WriteXml 方法

定义

DataSet 写 XML 数据,还可以选择写架构。

重载

WriteXml(XmlWriter, XmlWriteMode)

使用指定的 XmlWriterXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(String, XmlWriteMode)

使用指定的 XmlWriteModeDataSet 的当前数据和架构(可选)写入指定的文件。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(TextWriter, XmlWriteMode)

使用指定的 TextWriterXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(Stream, XmlWriteMode)

使用指定的 StreamXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

WriteXml(String)

DataSet 的当前数据写入指定的文件。

WriteXml(TextWriter)

使用指定的 DataSetTextWriter 写当前数据。

WriteXml(XmlWriter)

DataSet 的当前数据写入指定的 XmlWriter

WriteXml(Stream)

使用指定的 DataSetStream 写当前数据。

WriteXml(XmlWriter, XmlWriteMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 XmlWriterXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

public:
 void WriteXml(System::Xml::XmlWriter ^ writer, System::Data::XmlWriteMode mode);
public void WriteXml (System.Xml.XmlWriter? writer, System.Data.XmlWriteMode mode);
public void WriteXml (System.Xml.XmlWriter writer, System.Data.XmlWriteMode mode);
member this.WriteXml : System.Xml.XmlWriter * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (writer As XmlWriter, mode As XmlWriteMode)

参数

writer
XmlWriter

用于写入的 XmlWriter

mode
XmlWriteMode

XmlWriteMode 值之一。

示例

以下示例创建一个 System.IO.FileStream 对象,该对象用于创建新的 XmlTextWriter。 对象 XmlTextWriter 与 方法一起使用 WriteXml 以编写 XML 文档。

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();
}
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 = "XmlDoc.xml"

    ' Create the FileStream to write with.
    Dim stream As New System.IO.FileStream _
       (filename, System.IO.FileMode.Create)

    ' Create an XmlTextWriter with the fileStream.
    Dim xmlWriter As New System.Xml.XmlTextWriter _
       (stream, System.Text.Encoding.Unicode)

    ' Write to the file with the WriteXml method.
    thisDataSet.WriteXml(xmlWriter)
    xmlWriter.Close()
End Sub

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请将 mode 参数设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(String, XmlWriteMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 XmlWriteModeDataSet 的当前数据和架构(可选)写入指定的文件。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

public:
 void WriteXml(System::String ^ fileName, System::Data::XmlWriteMode mode);
public void WriteXml (string fileName, System.Data.XmlWriteMode mode);
member this.WriteXml : string * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (fileName As String, mode As XmlWriteMode)

参数

fileName
String

要向其写入的文件的名称(包括路径)。

mode
XmlWriteMode

XmlWriteMode 值之一。

例外

示例

以下示例使用 WriteXml 方法编写 XML 文档。

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);
}
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 = "XmlDoc.xml"

    ' Write to the file with the WriteXml method.
    thisDataSet.WriteXml(filename)
End Sub

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请将 mode 参数设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(TextWriter, XmlWriteMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 TextWriterXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

public:
 void WriteXml(System::IO::TextWriter ^ writer, System::Data::XmlWriteMode mode);
public void WriteXml (System.IO.TextWriter? writer, System.Data.XmlWriteMode mode);
public void WriteXml (System.IO.TextWriter writer, System.Data.XmlWriteMode mode);
member this.WriteXml : System.IO.TextWriter * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (writer As TextWriter, mode As XmlWriteMode)

参数

writer
TextWriter

TextWriter 对象用于写文档。

mode
XmlWriteMode

XmlWriteMode 值之一。

示例

以下示例首先创建一个包含一DataTable个 、两列和十行的简单 DataSet 。 架构 DataSet 和数据通过调用 WriteXml 方法写入磁盘。 创建第二 DataSet 个,并使用 ReadXml 方法填充架构和数据。

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();
        }
    }
}
Private Sub DemonstrateReadWriteXMLDocumentWithFileStream()
    ' Create a DataSet with one table and two columns.
    Dim originalDataSet As New DataSet("dataSet")
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
       Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)
    originalDataSet.Tables.Add(table)

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
        table.Rows.Add(newRow)
    Next i
    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.
    Dim xmlFilename As String = "XmlDocument.xml"
    Dim streamWrite As 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.
    Dim newDataSet As New DataSet("New DataSet")
       
    ' Read the XML document back in. 
    ' Create new FileStream to read schema with.
    Dim streamRead As 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")
End Sub
   
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    Dim row As DataRow
    Dim column As DataColumn
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)         
        For Each row In  table.Rows             
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " & _
                   row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请将 mode 参数设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(Stream, XmlWriteMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 StreamXmlWriteMode 写入 DataSet 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema

public:
 void WriteXml(System::IO::Stream ^ stream, System::Data::XmlWriteMode mode);
public void WriteXml (System.IO.Stream? stream, System.Data.XmlWriteMode mode);
public void WriteXml (System.IO.Stream stream, System.Data.XmlWriteMode mode);
member this.WriteXml : System.IO.Stream * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (stream As Stream, mode As XmlWriteMode)

参数

stream
Stream

用于写入文件的 Stream 对象。

mode
XmlWriteMode

XmlWriteMode 值之一。

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请将 mode 参数设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(String)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

DataSet 的当前数据写入指定的文件。

public:
 void WriteXml(System::String ^ fileName);
public void WriteXml (string fileName);
member this.WriteXml : string -> unit
Public Sub WriteXml (fileName As String)

参数

fileName
String

要向其写入的文件的名称(包括路径)。

例外

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请使用包含 mode 参数的重载之一,并将其值设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(TextWriter)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 DataSetTextWriter 写当前数据。

public:
 void WriteXml(System::IO::TextWriter ^ writer);
public void WriteXml (System.IO.TextWriter? writer);
public void WriteXml (System.IO.TextWriter writer);
member this.WriteXml : System.IO.TextWriter -> unit
Public Sub WriteXml (writer As TextWriter)

参数

writer
TextWriter

要进行写入的 TextWriter 对象。

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请使用包含 mode 参数的重载之一,并将其值设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(XmlWriter)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

DataSet 的当前数据写入指定的 XmlWriter

public:
 void WriteXml(System::Xml::XmlWriter ^ writer);
public void WriteXml (System.Xml.XmlWriter? writer);
public void WriteXml (System.Xml.XmlWriter writer);
member this.WriteXml : System.Xml.XmlWriter -> unit
Public Sub WriteXml (writer As XmlWriter)

参数

writer
XmlWriter

用于写入的 XmlWriter

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请使用包含 mode 参数的重载之一,并将其值设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于

WriteXml(Stream)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 DataSetStream 写当前数据。

public:
 void WriteXml(System::IO::Stream ^ stream);
public void WriteXml (System.IO.Stream? stream);
public void WriteXml (System.IO.Stream stream);
member this.WriteXml : System.IO.Stream -> unit
Public Sub WriteXml (stream As Stream)

参数

stream
Stream

用于写入文件的 Stream 对象。

示例

以下示例创建 一个 System.IO.FileStream 对象。 然后, 对象与 方法一起使用 WriteXml 来编写 XML 文档。

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);
}
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 = "XmlDoc.xml"

     ' Create the FileStream to write with.
     Dim stream As New System.IO.FileStream _
        (filename, System.IO.FileMode.Create)

     ' Write to the file with the WriteXml method.
     thisDataSet.WriteXml(stream)
End Sub

注解

方法 WriteXml 提供了一种只将数据或两者都从 DataSet 写入 XML 文档的方法,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请使用包含 mode 参数的重载之一,并将其值设置为 WriteSchema

请注意, ReadXmlReadXmlSchema 方法的这一点也不同。 若要将 XML 数据或架构和数据读入 , DataSet请使用 ReadXml 方法。 若要仅读取架构,请使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

另请参阅

适用于