zoukankan      html  css  js  c++  java
  • 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; }
        }
  • 相关阅读:
    总结在ssm整合中,Mybatis出现Mapped Statements collection already contains value for xxxxx的解决方案
    一般二叉树的创建,前序,中序,后序遍历
    无向图的广度优先遍历和深度优先遍历(简易实现)
    为什么局部内部类中访问同一方法中的变量,该变量一定要是final修饰的
    uml统一建模语言学习笔记(一)
    Font Awesome 字体使用方法, 兼容ie7+
    Java的三种代理模式&完整源码分析
    xxl-job源码分析
    MySQl看这一篇就够了
    第二部分:Spring中配置mongodb
  • 原文地址:https://www.cnblogs.com/Robbery/p/xmlhelper.html
Copyright © 2011-2022 走看看