zoukankan      html  css  js  c++  java
  • xml与DataSet互转

    //将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

  • 相关阅读:
    通道分离与合并
    opencv颜色表操作
    opencv trackbar
    像素操作
    opencv 像素读写
    py 时间与日期
    py 字典
    py 元组
    py 列表
    课后作业-阅读任务-阅读提问-3
  • 原文地址:https://www.cnblogs.com/mengzhixingping/p/12072705.html
Copyright © 2011-2022 走看看