XML操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Data;
using System.Globalization;
 
#endregion




namespace Pay.Util
{
    ///
    /// XML操作类
    ///

    public class XML
    {
        #region (public) xml序列化 _XMLSerialize
        ///
        /// xml序列化
        ///

        /// obj类
        /// string字符串
        public static string _XMLSerialize( object obj )
        {
            XmlSerializer xs = new XmlSerializer( obj.GetType() );
            StringBuilder strBuidler = new StringBuilder();
            XmlWriterSettings setting = new XmlWriterSettings();
            setting.OmitXmlDeclaration = true;//去掉xml版本声明
            System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create( strBuidler, setting );
            XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            xmlns.Add( string.Empty, string.Empty );
            xs.Serialize( xw, obj, xmlns );
            xw.Close();
            return strBuidler.ToString();
        }


        #endregion


        #region (public) xml序列化 _ConvertToString
        ///
        /// xml序列化
        ///

        /// obj类
        /// string字符串
        public static string _ConvertToString( object objectToConvert )
        {
            string xml = null;
            if ( objectToConvert == null )
                return xml;


            Type t = objectToConvert.GetType();


            XmlSerializer ser = new XmlSerializer( t );
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add( "", "" );
            using ( StringWriter writer = new StringWriter( CultureInfo.InvariantCulture ) )
            {
                ser.Serialize( writer, objectToConvert, ns );
                xml = writer.ToString();
                writer.Close();
            }
            return xml;
        }


        #endregion




        #region (public) xml反序列化 _XMLDeserialize
        ///
        /// xml反序列化
        ///

        /// 字符串string
        /// obj type
        /// obj
        public static object _XMLDeserialize( string s, Type type )
        {




            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml( s );
            s = xmlDoc.DocumentElement.OuterXml;
            XmlSerializer xs = new XmlSerializer( type );
            Stream stream = new System.IO.MemoryStream( System.Text.ASCIIEncoding.UTF8.GetBytes( s ) );
            object obj = xs.Deserialize( stream );
            stream.Close();
            return obj;
        }






        #endregion


        #region (public) xml反序列化 _XML2DataSet


        ///
        /// xml to dataset
        ///

        /// xml字符串
        /// dataset
        public static DataSet _XML2DataSet( string xmlData )
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            //try
            //{
            DataSet xmlDS = new DataSet();
            stream = new StringReader( xmlData );
            reader = new XmlTextReader( stream );
            xmlDS.ReadXml( reader );
            if ( reader != null )
            {
                reader.Close();
            }
            return xmlDS;
            //}
            //catch (Exception ex)
            //{
            //    string strTest = ex.Message;
            //    return null;
            //}
            //finally
            //{
            //    if (reader != null)
            //        reader.Close();
            //}
        }


        #endregion


        #region (public) XML反序列化 _ConvertFileToObject
        ///
        /// 读取文件转化为对象
        ///

        /// 路径
        /// 对象类型
        /// 对象
        public static object _ConvertFileToObject( string path, Type objectType )
        {
            object convertedObject = null;


            if ( path != null && path.Length > 0 )
            {
                using ( FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read ) )
                {
                    XmlSerializer ser = new XmlSerializer( objectType );
                    convertedObject = ser.Deserialize( fs );
                    fs.Close();
                }
            }
            return convertedObject;
        }


        #endregion


        #region (public) xml 反序列化 _ConvertXmlToObject
        ///
        /// 将xml字符串转换为对应的实体
        ///

        /// 实体类型
        /// xml字符串
        /// 实体类型对象
        public static T _ConvertXmlToObject( string xml ) where T : class, new()
        {
            if ( string.IsNullOrEmpty( xml ) )
                return new T();
            XmlSerializer serializer = new XmlSerializer( typeof( T ) );
            T resultObject;
            using ( TextReader reader = new StringReader( xml ) )
            {
                resultObject = (T)serializer.Deserialize( reader );
            }
            return resultObject;
        }


        #endregion


        #region(public) 对象保存为xml _SaveAsXML
        ///
        ///  把对象序列化为XML 并保存为文件
        ///

        /// 对象
        /// 路径
        public static void _SaveAsXML( object objectToConvert, string path )
        {
            if ( objectToConvert != null )
            {
                Type t = objectToConvert.GetType();
                XmlSerializer ser = new XmlSerializer( t );
                using ( StreamWriter writer = new StreamWriter( path ) )
                {
                    ser.Serialize( writer, objectToConvert );
                    writer.Close();
                }
            }
        }


        #endregion
    }
}

你可能感兴趣的:(NET,xml,序列化,反序列化)