zoukankan      html  css  js  c++  java
  • C#工具类之Xml序列化扩展类

    using System;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Xml.Serialization;
    /// <summary>
        /// Xml序列化
        /// </summary>
        public static class XmlSerializerHelper
        {
            /// <summary>
            /// 将对象序列化为Xml
            /// </summary>
            /// <typeparam name="T">对象实例对应的类.T的修饰符应当为Public</typeparam>
            /// <param name="obj">对象实例</param>
            /// <returns>序列化后的字符串</returns>
            public static string ToXml<T>(this T obj)
            {
                if (obj == null) throw new NullReferenceException();
    
                XmlSerializer serializer = new XmlSerializer(typeof(T));
    
                using (StringWriter writer = new StringWriter())
                {
                    serializer.Serialize(writer, obj);
                    return writer.ToString();
                }
            }
    
            /// <summary>
            /// 反序列化
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="xmlDocument"></param>
            /// <returns></returns>
            public static T ToInstanceFromXml<T>(this string xmlDocument)
            {
                using (StringReader reader = new StringReader(xmlDocument))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    var obj = serializer.Deserialize(reader);
                    if (obj is T)
                    {
                        return (T)obj;
                    }
    
                    return default(T);
                }
            }
    
            /// <summary>
            /// 将对象序列化为Xml
            /// </summary>
            /// <param name="obj">对象实例</param>
            /// <returns>序列化后的字符串</returns>
            public static string ToXml(this object obj)
            {
                return ToXml(obj, Encoding.UTF8);
            }
    
            /// <summary>
            /// 将对象序列化为Xml
            /// </summary>
            /// <param name="obj">对象实例</param>
            /// <param name="encoding">编码格式</param>
            /// <returns>序列化后的字符串</returns>
            public static string ToXml(this object obj, Encoding encoding)
            {
                if (obj == null)
                {
                    throw new ArgumentException("对象实例不能为空");
                }
    
                if (encoding == null)
                {
                    throw new Exception("编码不能为空");
                }
    
                using (var stream = new MemoryStream())
                {
                    var serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(stream, obj);
                    stream.Position = 0;
                    return encoding.GetString(stream.ToArray());
                }
            }
    
        }
  • 相关阅读:
    TypeScript 引入第三方包却报错:"无法找到模块"
    TS与hook useState
    原生js《发布订阅》功能
    react EUI 《消息通知》组件封装
    react 父级调用子级方法
    本人前端的面试笔记
    uniCloud云函数公共模块导入错误
    前端常见安全性问题
    2020 Qcon 深圳场参会感想
    嵌入式TF卡全备份与恢复嵌入式TF卡全备份与恢复
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/6668642.html
Copyright © 2011-2022 走看看