如何将DataGridView的数据保存为XML文件 急~~~ 高手们指点下谢谢.... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用DataSet转XML,DataSet由DataGridView的Datasource得到。 //将DataSet转换为xml文件 public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile) { MemoryStream stream = null; XmlTextWriter writer = null; try { stream = new MemoryStream(); //从stream装载到XmlTextReader writer = new XmlTextWriter(stream, Encoding.Unicode); //用WriteXml方法写入文件. xmlDS.WriteXml(writer); int count = (int)stream.Length; byte[] arr = new byte[count]; stream.Seek(0, SeekOrigin.Begin); stream.Read(arr, 0, count); //返回Unicode编码的文本 UnicodeEncoding utf = new UnicodeEncoding(); StreamWriter sw = new StreamWriter(xmlFile); sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sw.WriteLine(utf.GetString(arr).Trim()); sw.Close(); } catch( System.Exception ex ) { throw ex; } finally { if (writer != null) writer.Close(); } } DataTable dt..dt.WriteXml(file); DataTable 有个自带的方法。dt.WriteXml("E:/12.xml"); ds.WriteXml("Data.xml");for (int index = 0; index < this.dataGridView1.Rows.Count; index++) { DataGridViewRow row = this.dataGridView1.Rows[index]; CreateXmlNode(row); } 关于ArrayOfString VS2008连接数据库问题 如何获得上传文件的大小? sqlce 3.5的下载地址 水晶报表:如何能够动态的打印用户选择的需要打印的字段? 今天女朋友过生日,送上B/S进销存源代码一份(VB.net)。。。 请教一个关于以一个List集合为类属性时,get、set设置问题 关于身份证号码... 下面代码读取 中文网页 返回为 ??, 如何 解决 c#里面,怎么样给参数一个默认值? 关于正则表达式 C#发布的软件安装后,有更新的话又要卸载再安装,有什么好办法?
//将DataSet转换为xml文件
public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null; try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode); //用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count); //返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();
}
catch( System.Exception ex )
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
}
dt.WriteXml(file);
for (int index = 0; index < this.dataGridView1.Rows.Count; index++)
{
DataGridViewRow row = this.dataGridView1.Rows[index];
CreateXmlNode(row);
}