我写的有问题  麻烦哪位给一下:)/// <summary>
        /// serialize the current object into xml string
        /// </summary>
        /// <returns>the xml document string</returns>
        public string XmlSerialize()
        {
            XmlSerializer serializer = new XmlSerializer(InnerDataTable.DataSet.GetType());
            MemoryStream ms = new MemoryStream();
            StreamWriter writer = new StreamWriter(ms, Encoding.UTF8);            serializer.Serialize(writer, InnerDataTable.DataSet);
            writer.Flush();
            writer.Close();
            writer.Dispose();            UTF8Encoding encoding = new UTF8Encoding();
            string xml = new string(encoding.GetChars(ms.GetBuffer())); 
            ms.Dispose();            return xml;
        }
        /// <summary>
        /// Deserialize a object by a xml string
        /// </summary>
        /// <param name="xml">the xml string</param>
        /// <returns>the object</returns>
        public static DataSet XmlDeserialize(string xml)
        {
            char[] array = xml.ToCharArray();
            byte[] buf = new byte[array.Length * 2];
            for( int i = 0; i < array.Length; i++)
            {
                buf[i * 2] = (byte)((array[i] & 0xFF00) >> 8);
                buf[i * 2 + 1] = (byte)(array[i] & 0x00FF);
            }
            MemoryStream ms = new MemoryStream(buf);
            
                                    XmlSerializer serializer = new XmlSerializer(typeof(DataSet));
            DataSet ds = (DataSet)serializer.Deserialize(ms);
        
            ms.Dispose();            return ds;
        }

解决方案 »

  1.   

    datatable直接有相应的方法,不用自已写代码.
      

  2.   

    哪个方法啊? 我GetXml后  不知道怎么还原啊
      

  3.   

    WriteXml与ReadXml怎么与 string 连用啊?
      

  4.   

    我写的 还是有问题/// <summary>
            /// serialize the current object into xml string
            /// </summary>
            /// <returns>the xml document string</returns>
            public string XmlSerialize()
            {
                MemoryStream ms = new MemoryStream();
                StreamWriter writer = new StreamWriter(ms, Encoding.UTF8);
                InnerDataTable.DataSet.WriteXml(writer);
                writer.Flush();
                writer.Close();
                writer.Dispose();            UTF8Encoding encoding = new UTF8Encoding();
                string xml = new string(encoding.GetChars(ms.GetBuffer())); 
                ms.Dispose();            return xml;
            }怎么反序列化呢?