//将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. C#中DataTable新增列、删除列、更改列名、交换列位置

    一.新增列 1.1.新增列 /*新增列*/ dataTable.Columns.Add("列名称", Type.GetType("数据类型")); /*比如添加 ...

  2. 学习C语言哟

    之前一直用的vs,感觉还不错,现在新发现了 一个Lightly工具,非常好用,各种环境自动配置好 看着新奇,比codeblocks好多了,各种玩意儿一大堆,不过也都行,只是这个安装轻松点 开始我的第二 ...

  3. vue3插槽变化

    1.默认插槽还是跟以前一样 2.使用具名插槽时,子组件不变 以前的父组件掉用的时候 <template slot="example"> </template> ...

  4. 使用centos8.5配置一台YUM源服务器

    公司的生产环境部署的Centos8.5, 现在已经EOL了, 为了给生产和测试机继续提供yum/dnf服务, 特意在公司的内部机房部署了一套本地yum源. 环境:centos 8.5 1. 下载镜像 ...

  5. Security Bulletin: IBM WebSphere Application Server is vulnerable to a remote code execution vulnerability (CVE-2023-23477)

    Skip to content     Support     DownloadsDocumentationForumsCasesMonitoringManage support account   ...

  6. SVN创建自己的版本库

    1.创建版本库 第一:新建文件夹 第二:将新建文件与SVN建立关联(创建版本库) 直接选择OK 点击确定后文件夹图标也换了 该下的信息就是用来协助我们存储数据的(不是数据) 2.获取SVN库中的数据并 ...

  7. CF1786E题解

    容易为本题的弱化版CF1786C想出一个贪心: #include<bits/stdc++.h> using namespace std; #define int long long int ...

  8. 打卡node day06 ---登录和注册接口

    1, nodemon 自动更新代码 npm i -g nodemon 启动: nodemon server.js 2,注册接口 1)目录结构 2)server.js const express = r ...

  9. pandas常用方法之read_excel详解

    前期准备 准备测试数据如下: fl_path = r"C:\Users\Desktop\test.xlsx" dic = { 'num': ['001', '002', '003' ...

  10. C#读取XML字符串及将XML字符串反序列化为对象

    在开发中遇到调用接口范围XML格式结果情况,获取结果中我们需要的信息则可能需要这两种数据处理: 1.如何将xml字符串转换为xml对象,及查询想要的节点: 通过XmlDocument对象加载xml字符 ...