[★]. Net for XML serialization and deserialization

Source: Internet
Author: User
Tags object serialization

Serialization stores an object on the storage media or converts the object so that it can be transmitted over the network. After an object is serialized, You Want To deserialize it.

That is, the behavior of re-converting data to available objects. This type of function is used when an object must be blocked from one context to another, for example, when an object

When the app domain is crossed. Another example is the Web service-the client that the object is serialized on the server and sent to through the network, and then deserialized into a useful

Object.
From binary to XML, the. NET Framework provides developers with many serialization options, and even allows developers to create their own serialization routines. In this article, I will focus on

Serialization, and shows you how to use this built-in function.

XML serialization
One form of serialization provided by the. NET Framework is XML serialization. In this type of serialization, the object state is saved in XML format. This allows serialized objects to be different

Or even those that are not written in. net. Another advantage is that the serialized object is readable and writable for people. Therefore, the object is updated.

You can change the value of a wordpad.

XML serialization is often used to remotely control projects and Web Service projects, although you may find it elsewhere, such as dataset messages. In the query with XPath and predicate Method

When used together, XML serialization can be used to implement object-oriented databases-I willArticleIn this regard.

Use XML serialization
It is relatively easy to use the built-in XML serialization method in the. NET Framework. You only need to familiarize yourself with some classes and attributes to start using simple XML serialization:

System. xml. serialization namespace: contains the classes and functions required for XML serialization. This namespace should be placed at the top of the class serialized using XML

In the "using" command.
Xmlserializer class: Provides Object serialization and deserialization functions.
Xmlignore property: tells the xmlserializer class to skip the member you do not want to serialize. // This attribute only applies to class3.
[Serializable ()]
Public class class2
{
Private string _ username = string. empty;
Public String Username
{
Get
{
Return _ username;
}
Set
{
_ Username = value;
}
}
Public class3 vv;

}

[Serializable]
Public class class3
{
Private string _ KK = string. empty;

[Xmlignore] private string _ description = string. empty;
Public String kk
{
Set
{
_ KK = value;
}
Get
{
Return _ KK;
}
}
Public String description
{
Set
{
_ Description = value;
}
Get
{
Return _ description;
}
}
========================
// Public String description
//{
// Set
//{
// _ Description = value;
//}
// Get
//{
// Return _ description;
//}
//}
Public class3 (){}
Public class3 (string D)
{
_ Description = D;
}
============ Skip the member you do not want to serialize

}
======================================
Class2 CLS = new class2 ();
Cls. Username = "Xingming! ";
Cls. VV = new class3 ();
Cls. vv. kk = "lovelove ";
Cls. vv. Description = "miaoshu ";

Xmlserializer xs = new xmlserializer (typeof (class2 ));
Stream stream = new filestream ("F: \ 1.xml", filemode. Create, fileaccess. Write, fileshare. readwrite );
Xs. serialize (stream, CLS );
Stream. Close ();
======================================
Xmlserializer xs = new xmlserializer (typeof (class2 ));
Stream stream = new filestream ("F: \ 1.xml", filemode. Open, fileaccess. Read, fileshare. readwrite );
Class2 P = (class2) XS. deserialize (Stream );
MessageBox. Show (P. username. tostring ());

 

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.