码迷,mamicode.com
首页 > 其他好文 > 详细

xml序列化与反序列化工具

时间:2018-08-04 20:27:54      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:read   mem   linq   write   def   using   adt   sys   sci   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace ConsoleApplication1
{
    /// <summary>
    /// xml序列化与反序列化工具
    /// </summary>
    public class XmlSerializeUtil
    {
        /// <summary>
        /// xml反序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="xml"></param>
        /// <example>
        /// CustomerMapPageRoute customerRoute = XmlSerializeUtil.Deserialize(typeof(CustomerMapPageRoute), xmldoc.OuterXml) as CustomerMapPageRoute
        /// </example>
        /// <returns></returns>
        public static object Deserialize(Type type, string xml)
        {
            using (StringReader stringReader = new StringReader(xml))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(type);
                return xmlSerializer.Deserialize(stringReader);
            }
        }



        /// <summary>
        /// xml反序列化
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static object Deserialize(Type type, Stream stream)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            return xmlSerializer.Deserialize(stream);
        }

        /// <summary>
        /// xml反序列化泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <example>
        /// CustomerMapPageRoute obj1 = XmlSerializeUtil.Deserialize<CustomerMapPageRoute>(xmldoc.OuterXml);
        /// </example>
        /// <returns></returns>
        public static T Deserialize<T>(string xml)
        {
            using (StringReader stringReader = new StringReader(xml))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                return (T)xmlSerializer.Deserialize(stringReader);
            }
        }

        /// <summary>
        /// xml反序列化泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static T Deserialize<T>(Type type, Stream stream)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(stream);
        }

        /// <summary>
        /// 对象序列化为xml
        /// </summary>
        /// <param name="type"></param>
        /// <param name="obj"></param>
        /// <example>
        /// string xml = XmlSerializeUtil.Serializer(typeof(对象名), obj);
        /// </example>
        /// <returns></returns>
        public static string Serializer(Type type, object obj)
        {
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(type);

                    xmlSerializer.Serialize(stream, obj);
                    stream.Position = 0;
                   
                    using (StreamReader streamReader = new StreamReader(stream))
                    {
                        return streamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 对象序列化为xml
        /// </summary>
        /// <typeparam name="T">泛型对象</typeparam>
        /// <param name="t">对象</param>
        /// <returns>xml encoding="utf-8"</returns>
        /// <example>
        /// string xml = XmlSerializeUtil.Serializer<对象名>(obj); //encoding="utf-8"
        /// </example>
        public static string Serializer<T>(T t)
        {
            return Serializer<T>(t, Encoding.UTF8);
        }

        /// <summary>
        /// 对象序列化为xml
        /// </summary>
        /// <typeparam name="T">泛型对象</typeparam>
        /// <param name="t">对象</param>
        /// <param name="encoding">Encoding e = Encoding.UTF8 ASCII,Unicode,UTF32,UTF7,UTF8,Default
        /// Encoding.Default    <?xml version="1.0" encoding="gb2312"?>
        /// Encoding.UTF8       <?xml version="1.0" encoding="utf-8"?>
        /// </param>
        /// <example>
        /// string xml = XmlSerializeUtil.Serializer<对象名>(obj); //encoding="utf-8"
        /// string xml = XmlSerializeUtil.Serializer<对象名>(obj, Encoding.Default); //encoding="gb2312"
        /// </example>
        /// <returns>xml</returns>
        public static string Serializer<T>(T t, Encoding encoding)
        {
            if (t == null)
                throw new ArgumentException("object is null");
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            try
            {
                using(MemoryStream stream=new MemoryStream())
                {
                    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                    xmlWriterSettings.Encoding = encoding;

                    XmlWriter xmlWiter = XmlWriter.Create(stream,xmlWriterSettings);

                    XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
                    xsn.Add(string.Empty, string.Empty);

                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(xmlWiter, t, xsn);
                    xmlWiter.Close();
                    stream.Position = 0;
                    using(StreamReader streamReader=new StreamReader(stream,encoding))
                    {
                        return streamReader.ReadToEnd();
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

    }
}

 

xml序列化与反序列化工具

标签:read   mem   linq   write   def   using   adt   sys   sci   

原文地址:https://www.cnblogs.com/tiasys/p/9419567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!