zoukankan      html  css  js  c++  java
  • C# 将DataTable对象转换成XML字符串

            /// <summary>
            /// 将DataTable对象转换成XML字符串
            /// </summary>
            /// <param name="dt">DataTable对象</param>
            /// <returns>XML字符串</returns>
            public static string GetXmlByDataTable(DataTable dt)
            {
                if (dt != null)
                {
                    MemoryStream ms = null;
                    XmlTextWriter XmlWt = null;
                    try
                    {
                        ms = new MemoryStream();
                        //根据ms实例化XmlWt
                        XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
                        //获取ds中的数据
                        dt.WriteXml(XmlWt);
                        int count = (int)ms.Length;
                        byte[] temp = new byte[count];
                        ms.Seek(0, SeekOrigin.Begin);
                        ms.Read(temp, 0, count);
                        //返回Unicode编码的文本
                        UnicodeEncoding ucode = new 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 "";
                }
            }

  • 相关阅读:
    【并发编程】多线程并发最佳实践
    【并发编程】死锁
    【并发编程】【JDK源码】J.U.C--线程池
    【并发编程】【JDK源码】J.U.C--组件FutureTask、ForkJoin、BlockingQueue
    【JVM】关于类加载器准备阶段的一道面试题目
    【并发编程】【JDK源码】J.U.C--AQS 及其同步组件(2/2)
    【并发编程】线程安全策略
    JSP 9大内置对象详解
    Jquery中的bind(),live(),delegate(),on()绑定事件方式
    阻止事件冒泡
  • 原文地址:https://www.cnblogs.com/cc1120/p/8963810.html
Copyright © 2011-2022 走看看