How can I convert a DataTable to an XML file in C#?

前端 未结 3 1060
南笙
南笙 2020-11-30 08:14

I want to convert a DataTable to an XML file in C#. How can I do this?

3条回答
  •  盖世英雄少女心
    2020-11-30 08:48

    Another way to get this done is by adding the data table to dataset and calling the GetXml() on the dataset.In addition to this dataset is equipped with the WriteXml() and ReadXml() for writing/reading the XML directly to/from a file path or stream.

    DataSet ds = new DataSet();
    ds.Tables.Add(dt1); // Table 1
    ds.Tables.Add(dt2); // Table 2...
    ...
    string dsXml= ds.GetXml();
    ...
    using (StreamWriter fs = new StreamWriter(xmlFile)) // XML File Path
    {
          ds.WriteXml(fs);
    }
    

提交回复
热议问题