2015-08-28 87 views
0

在生成的XML中缺少空的數據表。如何爲數據集中爲空的數據表生成XML?

這是產生它的代碼:

servicedDataSet.WriteXml(targetFile, XmlWriteMode.IgnoreSchema)

有數據集中的DataTable的空沒有生成XML標記。

我需要XML才能擁有數據集中的所有數據表。 有什麼辦法可以包含空的數據表?

回答

0

我正試圖將數據集的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); 
}