C#实现XML文件与DataTable、Dataset互转

作者:農碼一生 时间:2021-06-07 04:00:27 

一、DataTable转XML

#region DataTableToXml
       /// <summary>
       /// 将DataTable对象转换成XML字符串
       /// </summary>
       /// <param name="ds">DataSet对象</param>
       /// <returns>XML字符串</returns>
       public static string DataTableToXml(DataTable dt,string sName)
       {
           if (dt != null)
           {
               MemoryStream ms = null;
               XmlTextWriter XmlWt = null;
               try
               {
                   ms = new MemoryStream();
                   //根据ms实例化XmlWt
                   XmlWt = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
                   //获取ds中的数据
                   dt.TableName = Sql.IsEmptyString(sName) ? "dt2xml" : sName;
                   dt.WriteXml(XmlWt, XmlWriteMode.WriteSchema);
                   int count = (int)ms.Length;
                   byte[] temp = new byte[count];
                   ms.Seek(0, SeekOrigin.Begin);
                   ms.Read(temp, 0, count);
                   //返回Unicode编码的文本
                   System.Text.UnicodeEncoding ucode = new System.Text.UnicodeEncoding();
                   string returnValue = ucode.GetString(temp).Trim();
                   return returnValue;
               }
               catch (System.Exception ex)
               {
                   throw ex;
               }
               finally
               {
                   //释放资源
                   if (XmlWt != null)
                   {
                       XmlWt.Close();
                       ms.Close();
                       ms.Dispose();
                   }
               }
           }
           else
           {
               return "";
           }
       }
       #endregion

二、XML转Dataset

方法A:

#region Xml To DataSet
       public static DataSet XmlToDataSet(string xmlString)
       {
           XmlDocument xmldoc = new XmlDocument();
           xmldoc.LoadXml(xmlString);
           StringReader stream = null;
           XmlTextReader reader = null;
           try
           {
               DataSet xmlDS = new DataSet();
               stream = new StringReader(xmldoc.InnerXml);
               reader = new XmlTextReader(stream);
               xmlDS.ReadXml(reader);
               reader.Close();
               return xmlDS;
           }
           catch (System.Exception ex)
           {
               reader.Close();
               throw ex;
           }
       }
       #endregion

方法B:

private static  DataSet XMLToDataset()
       {
           string strDBXMLFile = @"F:\TestDir\XML\DBTEST.XML";
           DataSet dsXML = new DataSet();
           dsXML.ReadXml(strDBXMLFile);
           //某个节点名称的所有节点内容
            DataTable dtOneNote = dsXML.Tables["SMT"];
           return dsXML;
       }

三、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();
               }
           }

来源:https://www.cnblogs.com/wml-it/p/15155173.html

标签:C#,XML,DataTable,Dataset,互转
0
投稿

猜你喜欢

  • Mybatis下的SQL注入漏洞原理及防护方法解析

    2022-06-30 18:38:29
  • C#编程获取资源文件中图片的方法

    2023-05-23 06:13:39
  • java读取excel文件的两种方法

    2022-08-24 16:55:45
  • 使用Nexus搭建Maven私服教程的方法步骤

    2023-03-05 18:56:58
  • c# 编写的简单飞行棋游戏

    2022-12-05 05:10:59
  • Java实现的zip工具类完整实例

    2021-06-22 07:16:17
  • java 中http请求为了防止乱码解决方案

    2023-08-09 07:59:33
  • 浅谈Java finally语句到底是在return之前还是之后执行(必看篇)

    2022-10-20 06:58:41
  • Springboot整合pagehelper分页功能

    2021-12-14 15:53:48
  • java实现Base64加密解密算法

    2023-11-25 08:07:27
  • java实现字符串和数字转换工具

    2021-08-28 15:50:19
  • C#计算矩阵的逆矩阵方法实例分析

    2021-11-06 18:24:29
  • JAVA多线程知识汇总

    2021-08-03 09:04:47
  • 浅谈JAVA如何生成UUID唯一标识

    2023-08-12 20:45:03
  • Unity实现俄罗斯方块游戏

    2023-05-30 21:07:22
  • Mybatis的几种传参方式详解

    2021-12-08 15:35:53
  • 解决logback的日志文件路径问题

    2023-06-13 10:48:09
  • 基于jstl 标签的使用介绍

    2021-10-01 13:48:36
  • 解决idea中yml文件图标问题及自动提示失效的情况

    2021-06-08 14:59:42
  • springboot 防止重复请求防止重复点击的操作

    2021-09-19 16:03:00
  • asp之家 软件编程 m.aspxhome.com