• 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; }
        }
  • 相关阅读:
    一、Docker前提介绍
    CentOS6.10忘记MySQL8密码的解决办法
    阿里云服务器结合域名解析部署springboot项目到Tomcat
    Javap指令集
    微服务之路(十一)spring cloud stream
    微服务之路(十)spring cloud zuul
    微服务之路(九)spring cloud feign
    微服务之路(八)spring cloud netflix hystrix
    python连接oracle数据库
    单元测试-unittest模块
  • 原文地址:https://www.cnblogs.com/Robbery/p/xmlhelper.html
Copyright © 2020-2023  润新知