编程语言
首页 > 编程语言> > C# XML 序列化帮助类

C# XML 序列化帮助类

作者:互联网

    /// <summary>
    ///     Xml helper class
    /// </summary>
    public static class XmlHelper
    {
        #region 序列化

        /// <summary>
        ///     XML Serialize
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Serialize<T>(T obj, Encoding encoding = null)
        {
            return Serialize(obj, typeof(T), encoding);
        }

        /// <summary>
        ///     XML Serialize
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string Serialize(object obj, Type type, Encoding encoding = null)
        {
            return Serialize(obj, type, encoding, null);
        }

        /// <summary>
        ///     XML Serialize
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        /// <param name="encoding"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public static string Serialize(object obj, Type type, Encoding encoding, params Type[] types)
        {
            var serializer = new XmlSerializer(type, types);
            var sb = new StringBuilder();

            using (var writer = new EncodingStringWriter(sb, encoding))
            {
                var namespaces = new XmlSerializerNamespaces();
                namespaces.Add("", "");
                serializer.Serialize(writer, obj, namespaces);
                return sb.ToString();
            }
        }

        /// <summary>
        ///     XML Deserialize
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml)
        {
            return Deserialize<T>(xml, typeof(T));
        }

        /// <summary>
        ///     XML Deserialize
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml, Type type)
        {
            return Deserialize<T>(xml, type, null);
        }

        /// <summary>
        ///     XML Deserialize
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <param name="type"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xml, Type type, params Type[] types)
        {
            var serializer = new XmlSerializer(type, types);
            using (TextReader reader = new StringReader(xml))
            {
                var obj = (T)serializer.Deserialize(reader);
                return obj;
            }
        }

        #endregion 序列化
    }

    public class EncodingStringWriter : StringWriter
    {
        public EncodingStringWriter(StringBuilder stringBuilder, Encoding encoding = null) : base(stringBuilder)
        {
            Encoding = encoding ?? Encoding.UTF8;
        }

        public override Encoding Encoding { get; }
    }

 

标签:XML,obj,Deserialize,encoding,C#,Serialize,序列化,type,public
来源: https://www.cnblogs.com/Robbery/p/xmlhelper.html