XML serialization and deserialization

Source: Internet
Author: User

This is a bad question, and there is a lot of information on the Internet. I will try again to give beginners a clear idea and code that can be run to reduce detours.

Let's talk about XML first.

Quote:

Extensible Markup Language (XML)
It is used to mark electronic files so that they have a structured markup language. It can be used to mark data and define data types. It is a source language that allows users to define their own markup language. XML is a subset of the standard General Markup Language (SGML) and is suitable for web transmission. XML provides a unified way to describe and exchange structured data independent of applications or vendors.

 

The purpose of XML serialization and deserialization is to serialize an object into XML and then forward it to another party for deserialization. In this way, data exchange is achieved, and the objects of both parties have no relationship. All the expressions are based on the agreed XML. So no matter whether you are. Net or Java, it doesn't matter. Is it easy to use?

 

Use the code.

1. constructor. Here I construct a school object, which contains two Majors: English and Chinese, two major teachers and classrooms, and information about students in the Major, as shown below:

[Serializable]
[Xmlroot ("school")]
Public class testobject
{
[Xmlattribute ("descri")]
Public String descri = "Eddy's XML serializaion Demo ";

[Xmlelement ("English")]
Public English;

[Xmlelement ("Chinese")]
Public Chinese;
}

Public class English
{
[Xmlelement ("teacher")]
Public String Teacher = "Eddy ";

[Xmlelement ("room")]
Public String room = "202 ";
}

Public class Chinese
{
[Xmlelement ("teacher")]
Public String Teacher = "Eddy ";

[Xmlelement ("room")]
Public String room = "203 ";

[Xmlelement ("Students")]
Public students;
}

Public class students
{
[Xmlelement ("student")]
Public list <student> student;
}

Public class student
{
[Xmlattribute ("name")]
Public string name;

[Xmlattribute ("no")]
Public String no;

[Xmlattribute ("Age")]
Public String age = "19 ";
}

 

2. Compile the serialization and deserialization methods as follows:

Public static string xmlserialization (Object OBJ)
{
String result = string. empty;

If (OBJ! = NULL)
{
Try
{
Xmlserializer = new xmlserializer (obj. GetType ());

Using (memorystream = new memorystream ())
{
Xmltextwriter writer = new xmltextwriter (memorystream, new utf8encoding (false ));

Writer. Formatting = formatting. indented;

Xmlserializer. serialize (memorystream, OBJ );

Writer. Flush ();
Writer. Close ();

Utf8encoding utf8encoding = new utf8encoding (false, true );

Result = utf8encoding. getstring (memorystream. toarray ());
}
}
Catch (exception ex)
{
Throw ex;
}
}

Return result;
}

Public static t xmldeserialization <t> (string XML)
{
T result = default (t );

Xmlserializer = new xmlserializer (typeof (t ));

Try
{
Using (stringreader = new stringreader (XML ))
{
Result = (t) xmlserializer. deserialize (stringreader );
}
}
Catch (exception ex)
{
Throw ex;
}

Return result;
}

 

3. Call example

Testobject OBJ = new testobject ()
{
Chinese = new Chinese ()
{
Students = new students ()
{
Student = new list <student> (){
New student (){
No = "1 ",
Name = "eddypeng ",
Age = "20"
},
New student (){
No = "2 ",
Name = "Eddy's friend ",
Age = "20"
}
}
}
},
English = new English ()
};

String xml = utility. xmlserialization (OBJ );

Testobject obj1 = utility. xmldeserialization <testobject> (XML );

 

It's easy. You don't need to worry about it. It's been written for half an hour... The code can be executed immediately...

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.