DataSet与Xml文件的互相转换
DataSet转换为xml文件
//将DataSet转换为xml文件
private 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.Flush();
sw.Close();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
}
XML与DataSet的相互转换类
送给大家一个XML与DataSet的相互转换的类:
XmlDatasetConvert 该类提供了四种方法:
1、将xml对象内容字符串转换为DataSet
2、将xml文件转换为DataSet
3、将DataSet转换为xml对象字符串
4、将DataSet转换为xml文件
XmlDatasetConvert.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
namespace XmlDesign
{
class XmlDatasetConvert
{
//将xml对象内容字符串转换为DataSet
public static DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
}
//将xml文件转换为DataSet
public static DataSet ConvertXMLFileToDataSet(string xmlFile)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlFile);
DataSet xmlDS = new DataSet();
stream = new StringReader(xmld.InnerXml);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
//xmlDS.ReadXml(xmlFile);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
}
//将DataSet转换为xml对象字符串
public static string ConvertDataSetToXML(DataSet xmlDS)
{
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);
UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
}
//将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();
}
}
}
}
使用示例
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
namespace XmlDesign
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();
转换一个XML文件(本地\网络均可)为一个DataSet#region 转换一个XML文件(本地\网络均可)为一个DataSet
//http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss
//F:\study\001CSharp_Study\002Source\XmlDesign\XmlDesign\Save_Plan.xml
ds = XmlDatasetConvert.ConvertXMLFileToDataSet(@"http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss");
Console.WriteLine("数据集名为\"{0}\",包含{1}个表", ds.DataSetName, ds.Tables.Count);
foreach(DataTable dt in ds.Tables)
{
PrintTableName(dt.TableName);
};
#endregion
构造一个DataSet,并转换为XML字符串#region 构造一个DataSet,并转换为XML字符串
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
dt1.TableName = "test";
dt1.Columns.Add("id");
dt1.Columns.Add("name");
dt1.Rows.Add("i001", "hekui");
dt1.Rows.Add("i002", "liyang");
DataTable dt2 = new DataTable();
dt2.TableName = "test1";
dt2.Columns.Add("bookid");
dt2.Columns.Add("bookname");
dt2.Rows.Add("b001", "书本1");
dt2.Rows.Add("b002", "书本2");
ds1.Tables.Add(dt1);
ds1.Tables.Add(dt2);
ds1.DataSetName = "方案";
string xmlOut = XmlDatasetConvert.ConvertDataSetToXML(ds1);
#endregion
转换一个XML字符串为一个DataSet#region 转换一个XML字符串为一个DataSet
DataSet ds2 = new DataSet();
ds2 = XmlDatasetConvert.ConvertXMLToDataSet(xmlOut);
Console.WriteLine("数据集名为\"{0}\",包含{1}个表", ds2.DataSetName, ds2.Tables.Count);
foreach (DataTable dt in ds2.Tables)
{
PrintTableName(dt.TableName);
};
#endregion
转换一个Dataset为一个XML文件#region 转换一个Dataset为一个XML文件
XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, "c:\\adadsda1.xml");
#endregion
Console.ReadLine();
}
private static void PrintTableName(string tableName)
{
Console.WriteLine(tableName);
}
}
}
DataSet与Xml文件的互相转换的更多相关文章
- c#解析XML到DATASET及dataset转为xml文件函数
//将xml对象内容字符串转换为DataSet public static DataSet ConvertXMLToDataSet(string xmlData) { ...
- C# datatable 与 xml文件之间的转换
/// <summary> /// datatable转XML文件 /// </summary> /// <param name="dtTable"& ...
- C# 实现DataTable、DataSet与XML互相转换
/**//// <summary> /// 把DataSet.DataTable.DataView格式转换成XML字符串.XML文件 /// </summary> public ...
- DataSet,DataTable,XML格式互转
//// <summary> /// 将DataTable对象转换成XML字符串 /// </summary> /// <param name="dt" ...
- C#对象与XMl文件之间的相互转换
C#提供三种序列化方式,分别为: 1.是使用BinaryFormatter进行串行化: 2.使用SoapFormatter进行串行化: 3.使用XmlSerializer进行串行化.其中对于Binar ...
- ASP.NET中把xml转为dataset与xml字符串转为dataset及dataset转为xml的代码
转自:http://www.cnblogs.com/_zjl/archive/2011/04/08/2009087.html XmlDatasetConvert.csusing System;usin ...
- 简单实体类和xml文件的相互转换
最近写一个题目,要求将一组员工实体类转换成xml文件,或将xml文件转换成一组实体类.题目不难,但写完感觉可以利用泛型和反射将任意一个实体类和xml文件进行转换.于是今天下午立马动手 试了下,做了个简 ...
- PLSQL解析XML文件
参考网上资料学习汇总 在PL/SQL中利用XML ,Oracle提供了几个组件,让开发人员能轻松地利用XML技术.这些组件包括: 1. XML 分析程序.即用来分析.构造和验证XML文档.. ...
- C#对象与XMl文件之间的相互转换(转)
本文是对C#中对象与XMl文件之间的相互转换进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 C#提供三种序列化方式,分别为:1.是使用BinaryFormatter进行串行化: 2.使 ...
随机推荐
- java随机生成简体中文取指定长度随机简体中文实用方法
/** * 获取指定长度随机简体中文 * @param len int * @return String */ public static String getR ...
- 一个简单的web服务器
写在前面 新的一年了,新的开始,打算重新看一遍asp.net本质论这本书,再重新认识一下,查漏补缺,认认真真的过一遍. 一个简单的web服务器 首先需要引入命名空间: System.Net,关于网络编 ...
- 我所使用的一个通用的Makefile模板
话不多说,请看: 我的项目有的目录结构有: dirls/ ├── include │ └── apue.h ├── lib │ ├── error.c │ ├── error.o │ ...
- Dean Edwards大神写的addEvent库
直接晒代码: // written by Dean Edwards, 2005 // with input from Tino Zijdel, Matthias Miller, Diego Perin ...
- Excel加密的Sheet如何hack
Excel的加密sheet如何hack: 思路:在VBA中添加穷举法模块函数并运行 源代码: Sub PasswordBreaker() 'Breaks worksheet password prot ...
- QQ微信与智能家电连接一起 小马哥"连接一切"野心凸显
昨日,彭博社对于海南举行的腾讯全球合作伙伴大会进行了报道,文章指出腾讯公司正在发力移动端,将其即时通讯工具QQ和微信与烤箱.电视.空调等其他家电连接在一起.小马哥"连接一切"的野心 ...
- WeakReference(弱引用)
原地址:http://www.cnblogs.com/bayonetxxx/archive/2009/06/02/1494728.html 我们平常用的都是对象的强引用,如果有强引用存在,GC是不会回 ...
- django 1.7 新特性 --- data migration
官方文档:https://docs.djangoproject.com/en/dev/topics/migrations/ 1.7 之前大家可能会用south用于管理数据库的模型的同步.1.7之后dj ...
- BeautifulSoup获取指定class样式的div
如何获取指定的标签的内容是解析网页爬取数据的必要手段,比如想获取<div class='xxx'> ...<div>这样的div标签,通常有三种办法, 1)用字符串查找方法,然 ...
- 如何实现SSH断开后 进程仍然在后台运行
1.nohup命令功能:不挂断地运行命令,忽略HUP信号.语法:nohup command & 实例:nohup ping www.google.com & 转自: http://bl ...