一、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(, SeekOrigin.Begin);
stream.Read(arr, , 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(, SeekOrigin.Begin);
stream.Read(arr, , count); //返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine("<?xml version=\\"1.0\\" encoding=\\"utf-\\"?>");
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("数据集名为\\"{}\\",包含{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的相互转换的类的更多相关文章

  1. XML与DataSet的相互转换

    转:https://www.cnblogs.com/kunEssay/p/6168824.html XML与DataSet的相互转换的类 一.XML与DataSet的相互转换的类 using Syst ...

  2. XML与DataSet相互转换,DataSet查询

    以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...

  3. [XML] C#XMLProcess操作Xml文档的帮助类 (转载)

    点击下载 XMLProcess.rar 主要功能如下所示 看下面代码吧 /// <summary> /// 类说明:XMLProcess /// 编 码 人:苏飞 /// 联系方式:361 ...

  4. java socket报文通信(三)java对象和xml格式文件的相互转换

    前两节讲了socket服务端,客户端的建立以及报文的封装.今天就来讲一下java对象和xml格式文件的相互转换. 上一节中我们列举了一个报文格式,其实我们可以理解为其实就是一个字符串.但是我们不可能每 ...

  5. 模拟在内存中的数据库DataSet相关的类

    这篇连着上一篇DataReader相关类. 下面两段话是在msdn官网摘下来:       .NET Framework 数据提供程序是专门为数据操作以及快速.只进.只读访问数据而设计的组件.Conn ...

  6. C#对象与XMl文件之间的相互转换(转)

    本文是对C#中对象与XMl文件之间的相互转换进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 C#提供三种序列化方式,分别为:1.是使用BinaryFormatter进行串行化: 2.使 ...

  7. 利用Xml架构生成实体访问类

    由xml生成xsd及实体类   xmldataset工具 使用VS2005工具XSD.exe(SDK/v2.0/Bin/xsd.exe)自动生成实体类: xsd /c /namespace:myCom ...

  8. 根据xml生成相应的对象类

    根据xml生成相应的class对象,听起来很难其实很简单,用xsd.exe就能办到 打开vs 命令行运行xsd.exe 你的xml文件地址 空格/outputdir:存放xsd的地址 ok,这是生成了 ...

  9. 【C#】菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类

    VS 2015菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类

随机推荐

  1. springBoot上传文件时MultipartFile报空问题解决方法

    springBoot上传文件时MultipartFile报空问题解决方法 1.问题描述: 之前用spring MVC,转成spring boot之后发现上传不能用.网上参考说是spring boot已 ...

  2. 【Java并发编程】:生产者—消费者模型

    生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据. 这里实现如下情况的生产--消费模型: 生产者不断交替地生产两组数据“姓 ...

  3. Spring Boot的Servlet简单使用

    当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet.Filter和Servlet规范的所有监听器(如HttpSessionListener监听器). Spri ...

  4. Ubuntu各版本的历史发行界面

    不多说,直接上干货! 总的网址是:http://releases.ubuntu.com/releases/  比如,选择的是UbuntuKylin,则点击 http://cdimage.ubuntu. ...

  5. 机器学习中的范数规则化之L0、L1与L2范数

    今天看到一篇讲机器学习范数规则化的文章,讲得特别好,记录学习一下.原博客地址(http://blog.csdn.net/zouxy09). 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化. ...

  6. python mysql uitl

    from traceback import format_exc import pymysql as MySQLdb class MySQLUtils(object): def get_db_conn ...

  7. WINCRIS的使用

    附件下载:http://pan.baidu.com/s/1sj7EsOH 1.首先大家先到官网上下载自己的BIOS , 准备一个U盘格式化成FAT/FAT32格式. (网上说U盘的大小不要超过2G,这 ...

  8. vue view 表单验证正常逻辑

    <template> <Form ref="formInline" :model="formInline" :rules="rule ...

  9. NOI2018 退役记

    退役预订... upd 果然就这么不光荣的退役了... 我居然考出了一场只有两题得分的比赛,我好菜啊... 不过高三充(tui)实(fei)的生活应该很有意思... 大家一起加油吧!!!

  10. Feign性能优化注意事项--超时

      Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 1. at feign ...