简体   繁体   中英

How to write C# DataSet object to XML with customized nodes structure?

I have a similar case - I receive a DataSet object with data tables willed with data (eg customers table ...) from an external module (done by other programmer).

I then save the data set object to an xml using the writeXml method - here is my code

public void Save(string myXmlFilePath)
{        
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = System.Text.Encoding.UTF8; 
    settings.Indent = true;
    settings.NewLineHandling = NewLineHandling.None;

    using (XmlWriter writer = XmlWriter.Create(myXmlFilePath, settings))
    {
        ExportObject.WriteXml(writer);
        writer.Close();
    }
}

I get the standard xml file output structure like this:

<NewDataSet>
    <Customers>...</Customers>
    <Customers>...</Customers>
<NewDataSet>`

However I want the structure like this

<Customers>
    <Customer>...</Customer>
    <Customer>...</Customer>
</Customers>`

How can I achieve this?

Specify the desired names to the dataset and the tables within it.

ExportObject.DataSetName = "Customers";
ExportObject.Tables[0].TableName = "Customer";

解决方法是让WriteXml随心所欲地写,然后将XSD转换为您喜欢的格式。

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