转: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的相互转换的更多相关文章

  1. XML与DataSet的相互转换的类

    一.XML与DataSet的相互转换的类 using System; using System.Collections.Generic; using System.Text; using System ...

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

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

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

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

  4. 两个Xml转换为DataSet方法(C#)

    ///通过传入的特定XML字符串,通过 ReadXml函数读取到DataSet中.protected static DataSet GetDataSetByXml(string xmlData){   ...

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

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

  6. C#把Xml转换为DataSet的两种方法

    转:https://blog.csdn.net/beyondqd/article/details/6724676 下面给出两个实现XML数据转成DataSet的两个方法. 第1种: //通过传入的特定 ...

  7. 利用jaxb实现xml和bean的相互转换

    1.使用jar包生成xsd文件 java -jar trang.jar a.xml a.xsd xml格式 生成的xsd文件 2.使用xjc命令生成bean文件 xjc a.xsd 生成的相关bean ...

  8. c#解析XML到DATASET及dataset转为xml文件函数

    //将xml对象内容字符串转换为DataSet         public static DataSet ConvertXMLToDataSet(string xmlData)         { ...

  9. XML 与 DataSet/DataTable 互相转换实例(C#)——转载

    // <summary>      /// XML形式的字符串.XML文江转换成DataSet.DataTable格式      /// </summary>      pub ...

随机推荐

  1. JDK并发包总结

    本文主要介绍jdk中常用的同步控制工具以及并发容器, 其结构如下: 同步控制工具类 ReentrantLock 简而言之, 就是自由度更高的synchronized, 主要具备以下优点. 可重入: 单 ...

  2. [个人项目] echarts 实现数据(tooltip)自动轮播插件

    前言 最近, 工作中要做类似这种的项目. 用到了百度的 echarts 这个开源的数据可视化的框架. 因为投屏项目不像PC端的WEB, 它不允许用户用鼠标键盘等交互. 有些图表只能看到各部分的占比情况 ...

  3. 全网最详细的Windows系统里PLSQL Developer 64bit的下载与安装过程(图文详解)

    不多说,直接上干货! ORACLE是数据库,有客户端和服务器: 其,具体下载,可见http://www.oracle.com/technetwork/database/enterprise-editi ...

  4. FormData序列化及file文件上传

    表单数据上传 情况一: 一.当表单文件处于无任何处理状态时,用submit提交直接上传; 但这种方式上传,数据无任何处理:(极少使用): 但是传统的表单提交会导致页面刷新,但是有些情况下,我们并不希望 ...

  5. 让浏览器兼容ES6语法(gulp+babel)

    使用gulp+babel搭建ES6环境 前言 我们查阅资料可以知道ECMAScript 2015(简称ES6)已经于2015年发布,由于用户使用的浏览器版本在安装的时候可能早于ES6的发布,而到了今天 ...

  6. Java Collection 学习

    定义:Java 作为面向对象语言,对象的操作必比然是重中之重.要操作一个对象容易,如果需要存储多个对象,则需要一个容器,存储多个对象可以使用数组,但是数组的长度是不可变的.所以有了集合的概念.Coll ...

  7. Webhook是什么、怎么理解

    Webhook是什么 我们想看看维基老大的解说: A webhook in web development is a method of augmenting or altering the beha ...

  8. c#关于路径的总结(转)

    来源:http://www.cnblogs.com/yugongmengjiutian/articles/5521165.html 前一段时间写代码时经常遇到获取路径问题,总是感觉有点乱,于是就总结了 ...

  9. 动态加载JS函数

    一般性的,当我们需要加载js文件的时候都会使用script标签来实现,类似于如下代码: 代码如下: <script type="text/javascript" src=&q ...

  10. 跨域CORS 头缺少 'Access-Control-Allow-Origin'

    今天遇到一个跨域的问题找了好久的资料错误如下: 解决之后: 控制层 加上这两行代码就好啦: @RequestMapping(value = "",method = RequestM ...