弹指一挥间

好好做事,学习待人 (大数据分析/.NET/JAVA)技术交流QQ:860280456; .NET/JAVA技术交流群:192028174

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

XML与Datatable互换的两个函数:

private string ConvertDataTableToXML(DataTable xmlDS)
    {
        MemoryStream stream = null;
        XmlTextWriter writer = null;
        try
        {
            stream = new MemoryStream();
            writer = new XmlTextWriter(stream, Encoding.Default);
            xmlDS.WriteXml(writer);
            int count = (int)stream.Length;
            byte[] arr = new byte[count];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(arr, 0, count);
            UTF8Encoding utf = new UTF8Encoding();
            return utf.GetString(arr).Trim();
        }
        catch
        {
            return String.Empty;
        }
        finally
        {
            if (writer != null) writer.Close();
        }
    } 
    private 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();
      }
    }
posted on 2013-03-12 21:22  v.e.n.u.s  阅读(1914)  评论(0编辑  收藏  举报