C # Serializing XML, developing common

Source: Internet
Author: User

Serialization operations are most familiar to developers.

Serialization is divided into: serialization and deserialization.

Serialization noun Interpretation: serialization is the process of converting an object 's state into a format that can be persisted or transmitted.

Relative to serialization is deserialization, which transforms a stream into an object. Together, these two processes make it easy to store and transfer data. This is where serialization is meant to be.

We can serialize objects into different formats, such asJSON serialization ,XML Serialization , binary serialization ,SOAP Serialization , and so on. These different formats are also designed to accommodate specific business needs.

A recent development effort requires an XML serialization operation,

The approximate requirement is that there is a system B that provides an external data access interface, and B system receives only XML format data.

Here are two ways to do this:

1. piece together into a corresponding format XML data format to the B system, then there is a problem, in the patchwork process will encounter the escape character, then to solve the problem of escape character, the efficiency is not high, the code looks very low.

2. The implementation of serialization operations, this is the highest efficiency, code beautiful, very hierarchical structure, maintenance is also convenient.

Next is the operation of XML serialization, very convenient, suitable for the development of use, I have made the tool class, easy to call and porting.

1. Create a tool folder Utils, create a class file XmlUtils.cs

Class-Specific code:

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 Stringbu Ilder ();            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 ();}}    }

The next step is to create the Xmlmodels folder and create the CreateInfoXml.cs class, which is the set of XML elements that need to be serialized

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")] Publi        C 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;} }}

Now everything is ready to be owed to the East wind. The next step is to use the tool class to serialize the 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,                    };

I created an entity class that can be created by itself, and I don't create it here.

Calling a serialization method in a tool class

var dataxml = Xmlutils.serialize (Model1, true);

The second argument is true because I only need the XML data, not the XML header file, and true is to remove the XML header file.

The end result is as follows:

<apas_info>
<serviceid>xx</serviceid>

<projectname>xx</projectname>

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

This completes the serialization operation.

C # Serializing XML, developing common

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.