C#序列化xml,开发常用

序列化操作对于开发人员来说最熟悉不过了。

序列化分为:序列化反序列化

序列化名词解释:序列化是将对象状态转换为可保持或传输的格式的过程。

与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。这就是序列化的意义所在。

我们可以把对象序列化为不同的格式,比如说,Json序列化XML序列化二进制序列化SOAP序列化等,以上这些不同的格式也都是为了适应具体的业务需求。

 

最近开发工作中需要一个xml序列化操作,

大概需求就是,有一个系统B提供了一个对外数据访问接口,B系统只接收xml的格式数据。

在这里有两种写法:

    1.拼凑成一个对应格式xml数据格式传给B系统,那么这样就有一个问题,在拼凑过程会遇到到转义字符,那么要去解决转义字符带来的问题,效率上也不高,代码看起来也很low.

    2.实现序列化操作,这样做效率是最高的,代码美观,很有层次结构,维护也方便。

 

接下来就是对于xml序列化的操作,很方便,适合开发中使用,我已经做成工具类,方便调用与移植。

   1.创建一个工具文件夹Utils,创建一个类文件XmlUtils.cs

      类具体代码:

   

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

namespace Myxs.Application.port.Utils
{
    public static class XmlUtils
    {
        public static string Serialize<T>(this T obj, bool omitXmlDeclaration = false)
        {
            var sb = new StringBuilder();
            using (var xw = XmlWriter.Create(sb, new XmlWriterSettings()
            {
                OmitXmlDeclaration = omitXmlDeclaration,
                ConformanceLevel = ConformanceLevel.Auto,
                Indent = true
            }))
            {
                var xs = new XmlSerializer(obj.GetType());
                xs.Serialize(xw, obj);
            }

            return sb.ToString();
        }

    }
}

  接下来就是创建XmlModels文件夹,创建createInfoXml.cs的类,这里是设置需要序列化的xml元素

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Myxs.Application.port.XmlModels
{
    [XmlRoot(ElementName = "apas_info")]
    public class createInfoXml
    {
        [XmlElement(ElementName = "serviceid")]
        public string Serviceid { get; set; }
        [XmlElement(ElementName = "projectname")]
        public string Projectname { get; set; }
        [XmlElement(ElementName = "applyname")]
        public string Applyname { get; set; }
        [XmlElement(ElementName = "mobile")]
        public string Mobile { get; set; }
        [XmlElement(ElementName = "phone")]
        public string Phone { get; set; }
        [XmlElement(ElementName = "address")]
        public string Address { get; set; }
        [XmlElement(ElementName = "postcode")]
        public string Postcode { get; set; }
        [XmlElement(ElementName = "email")]
        public string Email { get; set; }
        [XmlElement(ElementName = "contactman")]
        public string Contactman { get; set; }
        [XmlElement(ElementName = "legalman")]
        public string Legalman { get; set; }
        [XmlElement(ElementName = "idcard")]
        public string Idcard { get; set; }
        [XmlElement(ElementName = "create_time")]
        public string Create_time { get; set; }
        [XmlElement(ElementName = "receive_time")]
        public string Receive_time { get; set; }
    }
}

  现在万事俱备只欠东风了。接下来就是使用工具类了,对xml进行序列化操作。

   

var model1 = new createInfoXml
                    {
                        Serviceid = entity.serviceid,
                        Projectname = entity.projectname,
                        Applyname = entity.applyname,
                        Mobile = entity.mobile,
                        Phone = entity.phone,
                        Address = entity.address,
                        Postcode = entity.postcode,
                        Email = entity.email,
                        Contactman = entity.contactman,
                        Legalman = entity.legalman,
                        Idcard = entity.idcard,
                        Create_time = entity.create_time,
                        Receive_time = entity.receive_time,
                    };

  后面是我创建的一个实体类,这个实体类可以自己创建,我就不在这里创建了。

        调用工具类中的序列化方法

var dataxml = XmlUtils.Serialize(model1, true);

  第二参数为true的原因就是我只需要xml的数据,不行了xml的头文件,为true是为了去除xml头文件。

最终效果图如下:

<apas_info>
<serviceid>xx</serviceid>

<projectname>xx</projectname>

<applyname>xxx</applyname>
<mobile>xx</mobile>
.................
</apas_info>

  这就完成序列化操作了。

   

 

posted @ 2017-10-16 09:56  *小嘻嘻*  阅读(2486)  评论(1编辑  收藏  举报