//将DataSet转换为xml字符串
   public static string ConvertDataSetToXMLFile(DataSet xmlDS, Encoding encoding)
   {
       MemoryStream stream = null;
XmlTextWriter writer = null;
       string result = "<result>-3</result>";
       try
       {
           stream = new MemoryStream();
           //从stream装载到XmlTextReader
           writer = new XmlTextWriter(stream, encoding);
           //用WriteXml方法写入文件.
           xmlDS.WriteXml(writer);
           int count = (int)stream.Length;
           byte[] arr = new byte[count];
           stream.Seek(0, SeekOrigin.Begin);
           stream.Read(arr, 0, count);
           result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + encoding.GetString(arr).Trim();
       }
       catch { }
       finally
       {
           if (writer != null) writer.Close();
       }
       return result;
   }

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

转载自:https://blog.51cto.com/aonaufly/1298823

xml与DataSet互转的更多相关文章

  1. XML 和 List 互转类

    XML 和 List 互转类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

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

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

  3. JavaScript实现XML与JSON互转代码(转载)

    下面来分享一个关于JavaScript实现XML与JSON互转例子,这里面介绍了国外的三款xml转json的例子,希望这些例子能给你带来帮助. 最近在开发在线XML编辑器,打算使用JSON做为中间格式 ...

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

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

  5. Json、JavaBean、Map、XML之间的互转

    思路是JavaBean.Map.XML都可以用工具类很简单的转换为Json,进而实现互相转换 1.Map.XML与Json互转 mvn依赖 <dependency> <groupId ...

  6. JAVA中 XML与数据库互转 学习笔记三

    要求 必备知识 JAVA基础知识,XML基础知识,数据库的基本操作. 开发环境 MyEclipse10/MySql5.5 资料下载 源码下载   数据库在数据查询,修改,保存,安全等方面与其他数据处理 ...

  7. XML与DataSet的相互转换

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

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

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

  9. c++实现Xml和json互转【转】

    https://blog.csdn.net/kfy2011/article/details/51774242 1.下载c语言的cJson库源码,库很小,只有两个文件cJSON.c和cJSON.h.下载 ...

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

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

随机推荐

  1. Apollo配置中心拉取,通过单独打包解决 Get config services failed from http://阿里云局域网访问IP:8080/services/config?appId=MyAppId&ip=192.168.145.1 Cause Could not complete get operation

    Apollo配置中心拉取,通过单独打包解决 Get config services failed from http://阿里云局域网访问IP:8080/services/config?appId=M ...

  2. IDEA EduTools Plugin Learning Cause

    背景 编程培训需求,能够检测学生的输入内容与预期一致,有课程大纲 IDEA Plugin EduTools 是一个非常出色的培训工具,具备在IDE中学习,能够通过单元测试验证正确错误,能够设置用户输入 ...

  3. ipmitool使用报错Could not open device at /dev/ipmi0 or /dev/ipmi/0 or /dev/ipmidev/0: No such file or directory

    ipmitool使用报错处理 Could not open device at /dev/ipmi0 or /dev/ipmi/0 or /dev/ipmidev/0: No such file or ...

  4. uniapp安卓本地文件读取(html5+)

    plus.io.resolveLocalFileSystemURL(                    "_www/static/caise.json",//static下路径 ...

  5. Hyper-v 安装openwrt

    安装注意事项: 1.只能选一代,网卡可以使用新版2.网卡高级设置,MAC地址欺骗一定要选上,不选外部交换机不能上网.3.防火墙做wan口转发4.防火墙wan口,两个reject改为 accept . ...

  6. Docker学习笔记-02 常用命令

    1.启动类: 启动docker: systemctl start docker 停止docker: systemctl stop docker 重启docker: systemctl restart ...

  7. protobuf笔记

    protobuf的enum .proto的enum类型在编译后,是C++的enum

  8. Java-面向对象基础 对象和方法

    // 定义属性 String nick; String color; int age;// 定义类的方法 public void eat(){ System.out.println("在吃东 ...

  9. Perl报错you may need to install the Win32::Console module(转)

    ActivePerl-5.26.3.XXXX.msi安装后,命令行执行cpan,会出现如下提示而无法继续. Can't locate Win32/Console.pm in @INC (you may ...

  10. Codeforces Round #781 (Div. 2) - D. GCD Guess

    GCD + 位运算 [Problem - 1665D - Codeforces](https://codeforces.com/problemset/problem/1627/D) 题意 交互题,有一 ...