datata 和XML转换 3

来源:互联网 发布:波特兰州立大学知乎 编辑:程序博客网 时间:2024/04/29 01:46
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= newbyte[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;
       
returnnull;
      }
     
finally
      {
       
if (reader!= null)
        reader.Close();
      }
    }
0 0