DataTable & String互转

using System.Text;
using System.IO;
using System.Data;
using System.Xml;

        private string ConvertDataTableToXML(DataTable dt)
        {
            using(MemoryStream stream=new MemoryStream())
            {
                using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.Default))
                {
                    try
                    {
                        dt.WriteXml(writer);
                        byte[] bytes = new byte[(int)stream.Length];
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.Read(bytes ,0, (int)stream.Length);
                        return System.Text.Encoding.Default.GetString(bytes);
                    }
                    catch
                    {
                        return "";
                    }
                }
            }
        }

        private DataSet ConvertXMLToDataSet(string data)
        {
            using (StringReader stream = new StringReader(data))
            {
                using (XmlTextReader reader = new XmlTextReader(stream))
                {
                    try
                    {
                        DataSet ds = new DataSet();
                        ds.ReadXml(reader);
                        return ds;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }

你可能感兴趣的:(.NET,string,dataset,stream,byte,null)