如何在 C# 中将 DataTable 转换为 XML 文件? [英] How can I convert a DataTable to an XML file in C#?

查看:61
本文介绍了如何在 C# 中将 DataTable 转换为 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 C# 中将 DataTable 转换为 XML 文件.我该怎么做?

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

推荐答案

另一种完成此操作的方法是将数据表添加到数据集并调用数据集上的 GetXml().此外此数据集配备了 WriteXml()ReadXml() 用于直接向/从文件路径或流写入/读取 XML.

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);
}

这篇关于如何在 C# 中将 DataTable 转换为 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆