DataTable.WriteXmlSchema 方法

定义

DataTable 的当前数据结构以 XML 架构形式写入。

重载

WriteXmlSchema(Stream)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的流。

WriteXmlSchema(TextWriter)

使用指定的 TextWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。

WriteXmlSchema(String)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的文件。

WriteXmlSchema(XmlWriter)

使用指定的 XmlWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。

WriteXmlSchema(Stream, Boolean)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的流。 若要保存该表及其所有子代的架构,请将 writeHierarchy 参数设置为 true

WriteXmlSchema(TextWriter, Boolean)

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

WriteXmlSchema(XmlWriter, Boolean)

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

WriteXmlSchema(String, Boolean)

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

示例

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

备注

此示例演示如何使用 的某个重载版本 WriteXmlSchema 。对于可能提供的其他示例,请参阅各个重载主题。

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.WriteXmlSchema(writer, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXmlSchema(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 writer,
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}

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

==============================
Customer table, without hierarchy
==============================
<?xml version="1.0" encoding="utf-16"?>
<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="Ta
ble1">
    <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>
==============================
Customer table, with hierarchy
==============================
<?xml version="1.0" encoding="utf-16"?>
<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" msdata: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>

注解

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

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

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

WriteXmlSchema(Stream)

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

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的流。

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

参数

stream
Stream

要向其写入 XML 架构的流。

注解

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

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

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

另请参阅

适用于

.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

WriteXmlSchema(TextWriter)

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

使用指定的 TextWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。

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

参数

writer
TextWriter

用于写入的 TextWriter

注解

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

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

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

另请参阅

适用于

.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

WriteXmlSchema(String)

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

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的文件。

C#
public void WriteXmlSchema (string fileName);

参数

fileName
String

要使用的文件的名称。

注解

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

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

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

另请参阅

适用于

.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

WriteXmlSchema(XmlWriter)

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

使用指定的 XmlWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。

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

参数

writer
XmlWriter

要使用的 XmlWriter

注解

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

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

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

另请参阅

适用于

.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

WriteXmlSchema(Stream, Boolean)

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

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的流。 若要保存该表及其所有子代的架构,请将 writeHierarchy 参数设置为 true

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

参数

stream
Stream

要向其写入 XML 架构的流。

writeHierarchy
Boolean

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

注解

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

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

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

通常, WriteXmlSchema 方法仅为当前表写入架构。 若要编写当前表及其整个后代相关表的架构,请调用 参数设置为 true的方法writeHierarchy

另请参阅

适用于

.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

WriteXmlSchema(TextWriter, Boolean)

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

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

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

参数

writer
TextWriter

用于写入的 TextWriter

writeHierarchy
Boolean

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

示例

以下控制台应用程序创建两DataTable个实例,将每个实例添加到 ,DataSet创建一个相关的两个DataRelationTextWriter表,然后使用 WriteXmlSchema 方法将父表中包含的数据写入 到 。 该示例演示将 参数设置为 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.WriteXmlSchema(writer, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXmlSchema(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 writer, string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}

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

==============================
Customer table, without hierarchy
==============================
<?xml version="1.0" encoding="utf-16"?>
<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="Ta
ble1">
    <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>
==============================
Customer table, with hierarchy
==============================
<?xml version="1.0" encoding="utf-16"?>
<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" msdata: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>

注解

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

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

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

通常, WriteXmlSchema 方法仅为当前表写入架构。 若要编写当前表及其整个后代相关表的架构,请调用 参数设置为 true的方法writeHierarchy

另请参阅

适用于

.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

WriteXmlSchema(XmlWriter, Boolean)

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

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

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

参数

writer
XmlWriter

用于写入文档的 XmlWriter

writeHierarchy
Boolean

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

注解

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

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

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

通常, WriteXmlSchema 方法仅为当前表写入架构。 若要编写当前表及其整个后代相关表的架构,请调用 参数设置为 true的方法writeHierarchy

另请参阅

适用于

.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

WriteXmlSchema(String, Boolean)

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

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

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

参数

fileName
String

要使用的文件的名称。

writeHierarchy
Boolean

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

注解

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

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

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

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

另请参阅

适用于

.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