XML序列化和反序列化

上篇总结了下JSON的序列化和反序列化,博园中大牛给了很多牛叉的评论,学习了不少。

不过在上篇中忘了把json序列化和反序列化的另外一种方式写上去了,这里做个简单的补充:

Json篇:http://www.cnblogs.com/zhanghaomars/p/3557644.html

Json序列化和反序列化扩展方法实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace HelpClass.TypeHelp
{
    public static class JsonHelpExpand
    {
        public static string JsonSerializer<T>(this T t) where T : class
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            return jsonSerialize.Serialize(t);
        }

        public static T JsonDeserialize<T>(this string jsonString)
        {
            JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
            return (T)jsonSerialize.Deserialize<T>(jsonString);
        }
    }
}

Ok!接下来就是XML

1、  静态方法+泛型实现

XML工具类:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace HelpClass.TypeHelp
{
    public class XMLHelp
    {
        private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding, bool isnamespaces)
        {
            if (o == null)
                throw new ArgumentNullException("o");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            XmlSerializer serializer = new XmlSerializer(o.GetType());

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineChars = "
";
            settings.Encoding = encoding;
            settings.IndentChars = "    ";

            //不生成声明头
            settings.OmitXmlDeclaration = !isnamespaces;

            MemoryStream w = new MemoryStream();

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "");

            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                serializer.Serialize(writer, o, namespaces);
                writer.Close();

            }
        }

        /// <summary>
        /// 将一个对象序列化为XML字符串
        /// </summary>
        /// <param name="o">要序列化的对象</param>
        /// <param name="encoding">编码方式</param>
        /// <param name="isnamespaces">是否需要命名空间true:需要 false:不需要</param>
        /// <returns>序列化产生的XML字符串</returns>
        public static string XmlSerialize(object o, Encoding encoding, bool isnamespaces)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializeInternal(stream, o, encoding, isnamespaces);

                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        /// <summary>
        /// 从XML字符串中反序列化对象
        /// </summary>
        /// <typeparam name="T">结果对象类型</typeparam>
        /// <param name="s">包含对象的XML字符串</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>反序列化得到的对象</returns>
        public static T XmlDeserialize<T>(string s, Encoding encoding)
        {
            if (string.IsNullOrEmpty(s))
                throw new ArgumentNullException("s");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
            {
                using (StreamReader sr = new StreamReader(ms, encoding))
                {
                    return (T)mySerializer.Deserialize(sr);
                }
            }
        }

        /// <summary>
        /// 将一个对象按XML序列化的方式写入到一个文件
        /// </summary>
        /// <param name="o">要序列化的对象</param>
        /// <param name="path">保存文件路径</param>
        /// <param name="encoding">编码方式</param>
        /// <param name="isnamespaces">是否需要命名空间true:需要 false:不需要</param>
        public static void XmlSerializeToFile(object o, string path, Encoding encoding, bool isnamespaces)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");

            using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                XmlSerializeInternal(file, o, encoding,isnamespaces);
            }
        }

        /// <summary>
        /// 读入一个文件,并按XML的方式反序列化对象。
        /// </summary>
        /// <typeparam name="T">结果对象类型</typeparam>
        /// <param name="path">文件路径</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>反序列化得到的对象</returns>
        public static T XmlDeserializeFromFile<T>(string path, Encoding encoding)
        {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            string xml = File.ReadAllText(path, encoding);
            return XmlDeserialize<T>(xml, encoding);
        }
    }
}

在一个项目中,如果面临大量的XML(如调用大量返回XML的外部接口),这时就需要根据XML结构来逆向推导C#类型,然后才能使用序列化和反序列化的方法。

当需要根据XML结构逆向推导类型,我们需要了解一下类型定义与XML结构的映射关系。

映射关系只需要记住几个标签:

(1)       [XmlElement]

类中的属性或者字段在不标记下默认都会生成XmlElement(不加任何Attribute的情况下)该标签把类中的属性或者字段序列化为XML中的节点

Public class ddd

{

         [XmlElement]

         Public string d1{get;set;}

         Public string d2{get;set;}

}

ddd cd=new ddd{d1=”1”,d2=”2”}

<ddd><d1>1</d1><d2>2</d2></ddd>

(2)       [XmlAttribute]

类中的属性或者字段在标记为[XmlAttribute]的情况下会生成为XML中以以该类名为节点的属性

Public class ddd

{

         [XmlAttribute]

         Public string d1{get;set;}

         [XmlElement]

         Public string d2{get;set;}

}

ddd cd=new ddd{d1=”1”,d2=”2”}

<ddd d1=”1”><d2>2</d2></ddd>

(3)       [InnerText]

如果希望类型中的属性或者字段生成InnerText,需要在类型的成员上用[XmlText]

Public class ddd

{

         [XmlAttribute]

         Public string d1{get;set;}

         [InnerText]

         Public string d2{get;set;}

}

ddd cd=new ddd{d1=”1”,d2=”2”}

<ddd d1=”1”>2</ddd>

(4)       重命名

XmlAttribute,XmlElement允许接受一个别名用来控制生成节点的名称,类型的重命名用XmlType来实现

[XmlType(“ccc”)]

Public class ddd

{

         [XmlAttribute(“c1”)]

         Public string d1{get;set;}

         [XmlElement(“c2”)]

         Public string d2{get;set;}

}

ddd cd=new ddd{d1=”1”,d2=”2”}

<ccc c1=”1”><c2>2</c2></ccc>

(5)       序列化去掉XML命名空间及声明头(工具类中代码以有相关说明了)

注意:

(1)       当需要考虑使用XML时,先不要想着XML结构,先应该定义好数据类型。

(2)       列表节点不要使用[XmlElement],它会让所有子节点【升级】,显得结构混乱。

(3)       如果希望序列化的XML长度小一点,可以采用[XmlAttribute],或者指定一个更短小的别名(序列化为JSON进行传输也是一种选择)

(4)       不要在一个列表中输出不同的数据类型,这样的XML结构的可读性不好。

(5)       尽量使用UTF-8编码,不要使用GB2312编码。

(6)       列表节点不要使用[XmlElement],它会让所有子节点【升级】,显得结构混乱。

2、  扩展方法+泛型实现

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace HelpClass.TypeHelp
{
    public static class XMLHelpExpand
    {
        /// <summary>
        /// XML序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <param name="isnamespaces">是否需要命名空间true:需要 false:不需要</param>
        /// <returns></returns>
        public static string XmlSerializer<T>(this T o,bool isnamespaces)where T:class
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(o.GetType());

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineChars = "
";
            settings.Encoding = Encoding.UTF8;
            settings.IndentChars = "   ";

            //不生成声明头
            settings.OmitXmlDeclaration = !isnamespaces;

            MemoryStream w = new MemoryStream();

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "");

            using (XmlWriter writer = XmlWriter.Create(w, settings))
            {
                serializer.Serialize(writer, o, namespaces);
                writer.Close();
            }

            return Encoding.UTF8.GetString(w.ToArray());
        }

        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="XmlString"></param>
        /// <returns></returns>
        public static T XmlDeserialize<T>(this string XmlString)
        {
            if (string.IsNullOrEmpty(XmlString))
                throw new ArgumentNullException("s");

            XmlSerializer mySerializer = new XmlSerializer(typeof(T));

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XmlString)))
            {
                using (StreamReader sr = new StreamReader(ms, Encoding.UTF8))
                {
                    return (T)mySerializer.Deserialize(sr);
                }
            }
        }
    }
}

看完了上面的这些或许你已经对XML的序列化和反序列化有了一定的了解,今天就写到这里,下次总结一下别的。

原文地址:https://www.cnblogs.com/zhanghaomars/p/3560761.html