如何为数据集中为空的数据表生成 XML?

How to generate XML for datatables that are empty from a dataset?

空数据表在生成的 XML 中丢失。

这是生成它的代码:

servicedDataSet.WriteXml(targetFile, XmlWriteMode.IgnoreSchema)

没有为数据集中的空数据表生成 XML 个标签。

我需要 XML 来包含数据集中的所有数据表。 有什么方法可以包含空数据表吗?

我试图将数据集的 xml 导入 Excel。但这样做会产生一些问题,例如具有空值的表和列会丢失。我找到的解决方案是创建一个 xsd 文件和一个 xml 文件。

将 xsd 映射到 excel,然后使用 xml 导入数据。

我使用以下代码生成 xml 和 xsd:

// Build xml file path (target)
string targetFile = "";
targetFile = Path.Combine(this.txtTargetPath.Text, _servicedDataSet.DataSetName + ".xml");

// To import the xml file in Excel two files are being generated: xml and xsd. 
// When mapping the xml file to excel the columns and tables with null values goes missing.
// Mapping the xsd file solves the problem. Therefore, we are generating two separate xml and xsd files.
// xsd for the structure and xml for the data.

if (chkSchema.Checked == true)
{
    // Build xsd file path (xsdtarget)
    string xsdtargetFile = "";
    xsdtargetFile = Path.Combine(this.txtTargetPath.Text, _servicedDataSet.DataSetName + ".xsd");

    // Publish the dataset as XML
    _servicedDataSet.WriteXml(targetFile, XmlWriteMode.IgnoreSchema);

    // Creating the xsd file from the dataset.
    string schema = _servicedDataSet.GetXmlSchema();

    // Encoding utf-16 is not compatible with Excel. Changing utf-16 to utf-8 sorts the problem
    string result = schema.Replace("utf-16", "utf-8");
    System.IO.File.WriteAllText(xsdtargetFile, result);
}