我怎么能一个DataTable转换为C#中的XML文件? [英] How can I convert a DataTable to an XML file in C#?

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

问题描述

我想一个DataTable转换为C#中的XML文件。我怎样才能做到这一点?

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

推荐答案

要完成这件事的另一种方式是通过将数据表的数据集并调用的getXML()的dataset.In除了这个数据集配备了中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);
}

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

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