• 将xml字符串转换成dataset


    /**//// <summary>
        /// XML形式的字符串、XML文江转换成DataSet、DataTable格式
        /// </summary>
        public class XmlToData
        {
            /**//// <summary>
            /// 将Xml内容字符串转换成DataSet对象
            /// </summary>
            /// <param name="xmlStr">Xml内容字符串</param>
            /// <returns>DataSet对象</returns>
            public static DataSet CXmlToDataSet(string xmlStr)
            {
                if (!string.IsNullOrEmpty(xmlStr))
                {
                    StringReader StrStream = null;
                    XmlTextReader Xmlrdr = null;
                    try
                    {
                        DataSet ds = new DataSet();
                        //读取字符串中的信息
                        StrStream = new StringReader(xmlStr);
                        //获取StrStream中的数据
                        Xmlrdr = new XmlTextReader(StrStream);
                        //ds获取Xmlrdr中的数据               
                        ds.ReadXml(Xmlrdr);
                        return ds;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        //释放资源
                        if (Xmlrdr != null)
                        {
                            Xmlrdr.Close();
                            StrStream.Close();
                            StrStream.Dispose();
                        }
                    }
                }
                else
                {
                    return null;
                }
            }

    或者

    #region 将XML转换成DataSet
        /// <summary>
        /// 将XML转换成DataSet
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        private static DataSet ConvertXMLToDataSet(string xmlData)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmlData);
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                return xmlDS;
            }
            catch (Exception ex)
            {
                string strTest = ex.Message;
                return null;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        
        #endregion
     
     
     或者
    StringReader stream = null;
    XmlTextReader reader = null;
    DataTable dt = new DataTable ();
    stream = new StringReader(xmlStringData);
    reader = new XmlTextReader(stream);

    dt.ReadXml(reader);

    reader.Close();

  • 相关阅读:
    Linux 进程间通信(包含一个经典的生产者消费者实例代码)
    Linux多进程编程实例
    web开发中的mysql使用
    Linux 网络编程中的read和write函数正确的使用方式
    Linux 使用tcpdump观察arp通信过程
    Linux 开启echo等服务
    Linux send和recv
    自己动手写http服务器——主程序(三)
    自己动手写http服务器——线程池(一)
    http请求报文格式和响应报文格式
  • 原文地址:https://www.cnblogs.com/xingqiba/p/2962615.html
Copyright © 2020-2023  润新知