XML与DataSet的相互转换
转:https://www.cnblogs.com/kunEssay/p/6168824.html
XML与DataSet的相互转换的类
一、XML与DataSet的相互转换的类
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 构造一个DataSet,并转换为XML字符串
构造一个DataSet,并转换为XML字符串 转换一个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);
}
}
}
XML与DataSet的相互转换的更多相关文章
- XML与DataSet的相互转换的类
一.XML与DataSet的相互转换的类 using System; using System.Collections.Generic; using System.Text; using System ...
- XML与DataSet相互转换,DataSet查询
以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...
- java socket报文通信(三)java对象和xml格式文件的相互转换
前两节讲了socket服务端,客户端的建立以及报文的封装.今天就来讲一下java对象和xml格式文件的相互转换. 上一节中我们列举了一个报文格式,其实我们可以理解为其实就是一个字符串.但是我们不可能每 ...
- 两个Xml转换为DataSet方法(C#)
///通过传入的特定XML字符串,通过 ReadXml函数读取到DataSet中.protected static DataSet GetDataSetByXml(string xmlData){ ...
- C#对象与XMl文件之间的相互转换(转)
本文是对C#中对象与XMl文件之间的相互转换进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 C#提供三种序列化方式,分别为:1.是使用BinaryFormatter进行串行化: 2.使 ...
- C#把Xml转换为DataSet的两种方法
转:https://blog.csdn.net/beyondqd/article/details/6724676 下面给出两个实现XML数据转成DataSet的两个方法. 第1种: //通过传入的特定 ...
- 利用jaxb实现xml和bean的相互转换
1.使用jar包生成xsd文件 java -jar trang.jar a.xml a.xsd xml格式 生成的xsd文件 2.使用xjc命令生成bean文件 xjc a.xsd 生成的相关bean ...
- c#解析XML到DATASET及dataset转为xml文件函数
//将xml对象内容字符串转换为DataSet public static DataSet ConvertXMLToDataSet(string xmlData) { ...
- XML 与 DataSet/DataTable 互相转换实例(C#)——转载
// <summary> /// XML形式的字符串.XML文江转换成DataSet.DataTable格式 /// </summary> pub ...
随机推荐
- idea terminal 修改为git bash 不支持中文
1.idea terminal 修改为git bash 由于IDEA自带的Terminal工具是Windows命令窗口cmd,在开发过程中需要用到一些常用的命令操作时要不断的在IDEA和git bas ...
- MyCat配置文件详解--server.xml
server.xml包含mycat的系统配置信息,它有两个标签,分别是user和system,掌握system标签的各项配置属性是mycat调优的关键. <?xml version=" ...
- java-构建jar带哟参数提示的
使用command的cli包构建带有参数提示的jar包 需要引入command cli的依赖 <commons.version>1.2</commons.version> &l ...
- markdown简单入门
1.斜体和加粗: 使用下划线"_"或"*"括起来 _内容_ or *内容* 1个_ 或 * 都是斜体,2个则是加粗: 3个既斜体 又加粗,4个以上则没什么变化 ...
- superset--presto sql
1.hive的partition的相关查询,由于presto不支持vachar和int的自动转换,所以使用where的时候需要手动转换一下. #select count(*) from userlog ...
- FFmpeg简易播放器的实现-音频播放
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10068490.html 基于FFmpeg和SDL实现的简易视频播放器,主要分为读取视频文 ...
- HTTP 无法注册URL 进程不具有命名空间的访问权限
写WCF时在 host.Open(); 报错:HTTP 无法注册 URL http://+:9999/CalculatorService/.进程不具有此命名空间的访问权限(有关详细信息,请参见 htt ...
- .18-浅析webpack源码之compile流程-rules参数处理(1)
Tips:写到这里,需要对当初的规则进行修改.在必要的地方,会在webpack.config.js中设置特殊的参数来跑源码,例如本例会使用module:{rules:[...]}来测试,基本上测试参数 ...
- C# WebApi 中设置Session可用
在Global.acax中,添加下面方法 //设置session可用 public override void Init() { PostAuthenticateRequest += MvcAppli ...
- Java基础——字符编码
一.ASII 美国(国家)信息交换标准(代)码. 计算机中只有数字,一切都是用数字表示,屏幕上显示的一个一个的字符也不例外. 一个字节可表示的数字为0-255,足以显示键盘上的所有的字符 例如. a ...