简体   繁体   中英

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

The datatables that are empty goes missing in the XML that is generated.

This is the Code that generates it:

servicedDataSet.WriteXml(targetFile, XmlWriteMode.IgnoreSchema)

There are no XML tags generated for empty datatables in the dataset.

I need the XML to have all the datatables in the dataset. Is there any way to include the empty datatables?

I was trying to import the xml of a dataset into Excel. But doing so was creating problems like the tables and columns with a null value would go missing. The solution I found was to create an xsd file and an xml file.

Map the xsd to the excel and then import the data using the xml.

I used the following codes to generate the xml and 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM