DataTable.WriteXml 方法

定义

以 XML 形式写入 DataTable 的当前内容。

重载

WriteXml(TextWriter, Boolean)

通过指定的 TextWriter,按 XML 形式编写 DataTable 的当前内容。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(TextWriter, XmlWriteMode)

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

WriteXml(String, XmlWriteMode)

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

WriteXml(Stream, XmlWriteMode)

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

WriteXml(XmlWriter, Boolean)

通过指定的 XmlWriter,按 XML 形式编写 DataTable 的当前内容。

WriteXml(XmlWriter, XmlWriteMode)

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

WriteXml(Stream, XmlWriteMode, Boolean)

使用指定的 XmlWriteModeDataTable 的当前数据和架构(可选)写入指定的文件。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(TextWriter, XmlWriteMode, Boolean)

使用指定的 TextWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(String, Boolean)

使用指定的文件以 XML 形式写入 DataTable 的当前内容。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(Stream, Boolean)

通过指定的 Stream,按 XML 形式编写 DataTable 的当前内容。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(XmlWriter, XmlWriteMode, Boolean)

使用指定的 XmlWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(String)

使用指定的文件以 XML 形式写入 DataTable 的当前内容。

WriteXml(TextWriter)

通过指定的 TextWriter,按 XML 形式编写 DataTable 的当前内容。

WriteXml(Stream)

通过指定的 Stream,按 XML 形式编写 DataTable 的当前内容。

WriteXml(String, XmlWriteMode, Boolean)

使用指定的文件和 XmlWriteMode 写入 DataTable 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

WriteXml(XmlWriter)

通过指定的 XmlWriter,按 XML 形式编写 DataTable 的当前内容。

示例

以下控制台应用程序创建两 DataTable 个实例,将每个实例添加到 一个 DataSet,创建与两个 DataRelation 表相关的 ,然后使用 WriteXml 方法将父表中包含的数据写入 到 TextWriter。 该示例演示将 参数设置为 writeHierarchy 其每个值时的行为。

备注

此示例演示如何使用 WriteXml 的重载版本之一。 有关可能可用的其他示例,请参阅各个重载主题。

C#
static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter writer, string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}

该示例在控制台窗口中显示以下输出:

==============================
Customer table, without hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
</NewDataSet>
==============================
Customer table, with hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="Table2">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="OrderID" type="xs:int" />
                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />
                <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />

              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
      <xs:unique name="Table2_Constraint1" msdata:ConstraintName="Constraint1" m
sdata:PrimaryKey="true">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="OrderID" />
      </xs:unique>
      <xs:keyref name="CustomerOrder" refer="Constraint1">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="CustomerID" />
      </xs:keyref>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
  <Table2>
    <OrderID>1</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2003-12-02T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>2</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2004-01-03T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>3</OrderID>
    <CustomerID>2</CustomerID>
    <OrderDate>2004-11-13T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>4</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-16T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>5</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-22T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>6</OrderID>
    <CustomerID>4</CustomerID>
    <OrderDate>2004-06-15T00:00:00.0000000-07:00</OrderDate>
  </Table2>
</NewDataSet>

注解

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

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

备注

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

WriteXml(TextWriter, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

通过指定的 TextWriter,按 XML 形式编写 DataTable 的当前内容。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (System.IO.TextWriter? writer, bool writeHierarchy);
C#
public void WriteXml (System.IO.TextWriter writer, bool writeHierarchy);

参数

writer
TextWriter

用于写入内容的 TextWriter

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

示例

以下控制台应用程序创建两 DataTable 个实例,将每个实例添加到 一个 DataSet,创建与两个 DataRelation 表相关的 ,然后使用 WriteXml 方法将父表中包含的数据写入 到 TextWriter。 该示例演示将 参数设置为 writeHierarchytrue时的行为。

C#
static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter stream,
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(stream.ToString());
}

注解

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其所有子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(TextWriter, XmlWriteMode)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public void WriteXml (System.IO.TextWriter? writer, System.Data.XmlWriteMode mode);
C#
public void WriteXml (System.IO.TextWriter writer, System.Data.XmlWriteMode mode);

参数

writer
TextWriter

用于写入文档的 TextWriter

mode
XmlWriteMode

XmlWriteMode 值之一。

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(String, XmlWriteMode)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public void WriteXml (string fileName, System.Data.XmlWriteMode mode);

参数

fileName
String

将向其写入数据的文件名称。

mode
XmlWriteMode

XmlWriteMode 值之一。

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(Stream, XmlWriteMode)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public void WriteXml (System.IO.Stream? stream, System.Data.XmlWriteMode mode);
C#
public void WriteXml (System.IO.Stream stream, System.Data.XmlWriteMode mode);

参数

stream
Stream

要向其写入数据的流。

mode
XmlWriteMode

XmlWriteMode 值之一。

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(XmlWriter, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

通过指定的 XmlWriter,按 XML 形式编写 DataTable 的当前内容。

C#
public void WriteXml (System.Xml.XmlWriter? writer, bool writeHierarchy);
C#
public void WriteXml (System.Xml.XmlWriter writer, bool writeHierarchy);

参数

writer
XmlWriter

用于写入内容的 XmlWriter

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

注解

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

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其整个子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(XmlWriter, XmlWriteMode)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public void WriteXml (System.Xml.XmlWriter? writer, System.Data.XmlWriteMode mode);
C#
public void WriteXml (System.Xml.XmlWriter writer, System.Data.XmlWriteMode mode);

参数

writer
XmlWriter

用于写入文档的 XmlWriter

mode
XmlWriteMode

XmlWriteMode 值之一。

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(Stream, XmlWriteMode, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

使用指定的 XmlWriteModeDataTable 的当前数据和架构(可选)写入指定的文件。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (System.IO.Stream? stream, System.Data.XmlWriteMode mode, bool writeHierarchy);
C#
public void WriteXml (System.IO.Stream stream, System.Data.XmlWriteMode mode, bool writeHierarchy);

参数

stream
Stream

要向其写入数据的流。

mode
XmlWriteMode

XmlWriteMode 值之一。

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

注解

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

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

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

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其所有子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(TextWriter, XmlWriteMode, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

使用指定的 TextWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (System.IO.TextWriter? writer, System.Data.XmlWriteMode mode, bool writeHierarchy);
C#
public void WriteXml (System.IO.TextWriter writer, System.Data.XmlWriteMode mode, bool writeHierarchy);

参数

writer
TextWriter

用于写入文档的 TextWriter

mode
XmlWriteMode

XmlWriteMode 值之一。

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

示例

以下控制台应用程序创建两 DataTable 个实例,将每个实例添加到 一个 DataSet,创建与两个 DataRelation 表相关的 ,然后使用 WriteXml 方法将父表中包含的数据写入 到 TextWriter。 该示例演示将 参数设置为 writeHierarchy 其每个值时的行为。

C#
static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter writer,
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}

该示例在控制台窗口中显示以下输出:

==============================
Customer table, without hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
</NewDataSet>
==============================
Customer table, with hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="Table2">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="OrderID" type="xs:int" />
                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />
                <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />

              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
      <xs:unique name="Table2_Constraint1" msdata:ConstraintName="Constraint1" m
sdata:PrimaryKey="true">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="OrderID" />
      </xs:unique>
      <xs:keyref name="CustomerOrder" refer="Constraint1">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="CustomerID" />
      </xs:keyref>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
  <Table2>
    <OrderID>1</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2003-12-02T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>2</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2004-01-03T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>3</OrderID>
    <CustomerID>2</CustomerID>
    <OrderDate>2004-11-13T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>4</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-16T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>5</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-22T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>6</OrderID>
    <CustomerID>4</CustomerID>
    <OrderDate>2004-06-15T00:00:00.0000000-07:00</OrderDate>
  </Table2>
</NewDataSet>

注解

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

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

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

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其所有子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(String, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

使用指定的文件以 XML 形式写入 DataTable 的当前内容。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (string fileName, bool writeHierarchy);

参数

fileName
String

要向其写入 XML 数据的文件。

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

注解

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

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其所有子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(Stream, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

通过指定的 Stream,按 XML 形式编写 DataTable 的当前内容。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (System.IO.Stream? stream, bool writeHierarchy);
C#
public void WriteXml (System.IO.Stream stream, bool writeHierarchy);

参数

stream
Stream

要向其写入数据的流。

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

注解

WriteXmlSchema使用 方法将 的DataTable架构写入 XML 文档。 架构包括表、关系和约束定义。

XML 架构是使用 XSD 标准编写的。

若要将数据写入 XML 文档,请使用 WriteXml 方法。

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其所有子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(XmlWriter, XmlWriteMode, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

使用指定的 XmlWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (System.Xml.XmlWriter? writer, System.Data.XmlWriteMode mode, bool writeHierarchy);
C#
public void WriteXml (System.Xml.XmlWriter writer, System.Data.XmlWriteMode mode, bool writeHierarchy);

参数

writer
XmlWriter

用于写入文档的 XmlWriter

mode
XmlWriteMode

XmlWriteMode 值之一。

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

注解

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

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表及其整个子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

使用指定的文件以 XML 形式写入 DataTable 的当前内容。

C#
public void WriteXml (string fileName);

参数

fileName
String

要向其写入 XML 数据的文件。

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(TextWriter)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

通过指定的 TextWriter,按 XML 形式编写 DataTable 的当前内容。

C#
public void WriteXml (System.IO.TextWriter? writer);
C#
public void WriteXml (System.IO.TextWriter writer);

参数

writer
TextWriter

用于写入内容的 TextWriter

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(Stream)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

通过指定的 Stream,按 XML 形式编写 DataTable 的当前内容。

C#
public void WriteXml (System.IO.Stream? stream);
C#
public void WriteXml (System.IO.Stream stream);

参数

stream
Stream

要向其写入数据的流。

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(String, XmlWriteMode, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

使用指定的文件和 XmlWriteMode 写入 DataTable 的当前数据和架构(可选)。 若要写入架构,请将 mode 参数的值设置为 WriteSchema。 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 true

C#
public void WriteXml (string fileName, System.Data.XmlWriteMode mode, bool writeHierarchy);

参数

fileName
String

将向其写入数据的文件名称。

mode
XmlWriteMode

XmlWriteMode 值之一。

writeHierarchy
Boolean

如果为 true,则写入当前表及其所有子代的内容。 如果为 false(默认值),则只写入当前表的数据。

注解

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

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

通常, WriteXml 方法仅保存当前表的数据。 如果要保存当前表的数据和所有架构,则 WriteXml 方法提供一种方法,用于仅将数据或同时将数据和架构从 DataTable 写入 XML 文档,而 WriteXmlSchema 该方法只写入架构。 若要同时写入数据和架构,请使用包含 XmlWriteMode 参数的重载之一,并将其值设置为 WriteSchema

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

通常, WriteXml 方法只写入当前表的数据。 若要写入当前表和所有子代相关表的数据,请调用 方法,并将 writeHierarchy 参数设置为 true

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(XmlWriter)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

通过指定的 XmlWriter,按 XML 形式编写 DataTable 的当前内容。

C#
public void WriteXml (System.Xml.XmlWriter? writer);
C#
public void WriteXml (System.Xml.XmlWriter writer);

参数

writer
XmlWriter

用于写入内容的 XmlWriter

注解

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

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1