Serialization in. NET (2)

Source: Internet
Author: User

Navigation:

Serialization in. Net (1)

Bytes ----------------------------------------------------------------------------------------------------------------------------

When talking about the value members in the serialization and deserialization classes, we also know that the member in the class is not Dangdang, but only the value member and the reference type, now we will introduce how to serialize referenced types, non-sequent files, and serialized files in the soap format.

Serialization reference type

By default, a serialization class requires all its members to be serializable (except for events, delegates, and explicit definitions that cannot be serialized)

Now I want to transform the previous example. The example is as follows:

    [Serializable]
public class MyClass
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public Work MyWork
{
get;
set;
}
}

[Serializable]
public class Work
{
public string Name
{
set;
get;
}
public string Address
{
set;
get;
}
}

In fact, it is very easy to add all the class members in the myclass class as the [serializable] attribute.. NET will traverse all the members in the myclass and intelligently skip the class circular reference. If the work type in myclass does not contain the [serializable] attribute, that is, the work cannot be serialized. NET will report an error.

Member not serializable

If you have a member of The sqlconnection type in myclass, we all know that sqlconnection manages database connections and is difficult to serialize. Therefore, we cannot serialize sqlconnection during design, we need to add the [nonserialized] attribute to the sqlconnection member. net class. When deserialization is performed, sqlconnection is null. For example:

    [Serializable]
public class MyClass
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
[NonSerialized]
Work myWork;

public Work MyWork
{
get
{
return myWork;
}
set
{
myWork = value;
}
}
}


public class Work
{
public string Name
{
set;
get;
}
public string Address
{
set;
get;
}
}

In this way, the work object will not be serialized into the file stream. When deserialized, The mywork attribute will be null.

Serialize a SOAP File

To serialize to soap, you must first reference the soapformatter class in the system. runtime. serialization. formatters. Soap namespace. Soapformatter is a soap formatter.

Example:

[Serializable]
Public class myclass
{
Public string name
{
Get;
Set;
}
Public int age
{
Get;
Set;
}
Public Work mywork
{
Get;
Set;
}
}

[Serializable]
Public class work
{
Public string name
{
Set;
Get;
}
Public String address
{
Set;
Get;
}
}

Class Program
{
Static void main (string [] ARGs)
{
MyClass my = new MyClass ();
My. Name = "Fengjie ";
My. Age = int. MaxValue;

Work work = new Work ();
Work. Address = "your next door ";
Work. Name = "unknown ";
My. MyWork = work;

// Here the new is in the Soap format
System. Runtime. Serialization. IFormatter formatter = new SoapFormatter ();

// Create a file stream
Stream stream = new FileStream (@ "c: \ MyClass. xml", FileMode. Create, FileAccess. Write );

Using (stream)
{
// Serialization is performed here.
Formatter. Serialize (stream, my );
}
}
}

In this way, the "Fengjie" object is serialized in the SOAP format and generated into your drive C (you can be depressed). The file content is as follows:

<SOAP-ENV: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: SOAP-ENC = "http://schemas.xmlsoap.org/soap/encoding/" xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" xmlns: clr = "http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV: encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/">
SOAP-ENV: Body>
<A1: MyClass id = "ref-1" xmlns: a1 = "http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/ConsoleApplication1%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_ X003C_Name_x003E_k _ BackingField id = "ref-3"> Fengjie </_ x003C_Name_x003E_k _ BackingField>
<_ X003C_Age_x003E_k _ BackingField> 2147483647 </_ x003C_Age_x003E_k _ BackingField>
<_ X003C_MyWork_x003E_k _ BackingField href = "# ref-4"/>
</A1: MyClass>
<A1: Work id = "ref-4" xmlns: a1 = "http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/ConsoleApplication1%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_ X003C_Name_x003E_k _ BackingField id = "ref-5"> unknown </_ x003C_Name_x003E_k _ BackingField>
<_ X003C_Address_x003E_k _ BackingField id = "ref-6"> next door to your home </_ x003C_Address_x003E_k _ BackingField>
</A1: Work>
SOAP-ENV: Body>
SOAP-ENV: Envelope>

Serialized files in the SOAP format are readable, so that cross-platform deserialization can be better performed. However, in terms of speed, it is still faster to use binary files, but it is difficult to perform cross-platform deserialization for binary files. Each has its own advantages.

This section is complete. In the next section, we will explain how to use the generic formatter to serialize the object.


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.