如何将dataset直接转化为XML文件流

sardineany 2010-07-05 03:41:42
RT。。。
...全文
120 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
tashiwoweiyi 2010-07-05
  • 打赏
  • 举报
回复
我是来学习的
king_wen 2010-07-05
  • 打赏
  • 举报
回复
飘过~~~~~~~~~~~
捷哥1999 2010-07-05
  • 打赏
  • 举报
回复
捷哥1999 2010-07-05
  • 打赏
  • 举报
回复
可以使用XmlDataDocument 直接操作数据集的数据,存储为xml!

public void SingleTable2XML()
{
//Create an ODBC connection to the database. Here it is an Access file

OdbcConnection conn = new OdbcConnection("DSN=XmlDb_NorthWind");

//Create a DataSet with a name "XmlDb"

DataSet dataset = new DataSet("XmlDb");

//Create a DataAdapter to load data from original data source to the DataSet

OdbcDataAdapter adapter = new OdbcDataAdapter();
adapter.SelectCommand = new OdbcCommand("SELECT * FROM Customers", conn);
adapter.Fill(dataset, "Customers");

//Create a virtual XML document on top of the DataSet

XmlDataDocument doc = new XmlDataDocument(dataset);

//Output this XML document

doc.Save(Console.Out);

//NUnit test to confirm the result is exactly what we expect

Assert.AreEqual("XmlDb", doc.DocumentElement.LocalName);
Assert.AreEqual("Customers", doc.DocumentElement.FirstChild.LocalName);
}
luxi0194 2010-07-05
  • 打赏
  • 举报
回复
ds.WriteXml()
bryht 2010-07-05
  • 打赏
  • 举报
回复
序列化
反序列化?
q107770540 2010-07-05
  • 打赏
  • 举报
回复

//将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();
}
}

}



参考:
不懂装懂 2010-07-05
  • 打赏
  • 举报
回复
ds.writexml(路径)?????
不懂装懂 2010-07-05
  • 打赏
  • 举报
回复
ds.writexml(路径)?????

110,555

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧