Application of serialization and deserialization in. Net in ASP. NET forums

Source: Internet
Author: User
Tags net serialization
Application of serialization and deserialization in. Net in ASP. NET forums

In forums, some content is not fixed, such as user information. In addition to some basic information, there may be other information, such as MSN, personal homepage, and signature file, generally, each attribute corresponds to a field in the database. However, if we need to add some attributes, such as the QQ number and blog address, if we still use this method to add data table fields, the database table structure, stored procedures, and database access programs will be modified frequently.

You may have encountered a similar problem. See how to solve it by using. Net serialization and deserialization in forums:
For example, if you need to add the QQ number attribute to the user information, you only need to add an attribute to the user class.
Public String qqim
{
Get {return getextendedattribute ("qqim ");}
Set {setextendedattribute ("qqim", value );}
}
No need to modify the database table structure, no need to modify the stored procedure, and no need to change the database access program.

The main code is as follows:

// Create a new namevaluecollection object in the user class and save these extension attributes in the namevaluecollection object.
Namevaluecollection extendedattributes = new namevaluecollection ();

// Retrieve records from namevaluecollection
Public String getextendedattribute (string name)
{
String returnvalue = extendedattributes [name];

If (returnvalue = NULL)
Return string. empty;
Else
Return returnvalue;
}

// Set the key value and value of the extended property in namevaluecollection.
Public void setextendedattribute (string name, string value)
{
Extendedattributes [name] = value;
}

// Serialize the extendedattributes object (the namevaluecollection object previously defined to save all user extension information) into a memory stream
// It can be saved to the database.
Public byte [] serializeextendedattributes ()
{

// Serialize the object
Binaryformatter = new binaryformatter ();

// Create a memory stream, which is serialized and saved in it
Memorystream MS = new memorystream ();
Byte [] B;

// Serialize the extendedattributes object (which stores all user Extension Information) into a memory stream
//
Binaryformatter. serialize (MS, extendedattributes );

// Set the starting position of the memory stream
//
Ms. Position = 0;

// Read to byte array
//
B = new byte [Ms. Length];
Ms. Read (B, 0, B. Length );
Ms. Close ();

Return B;
}

// Deserialize the content of the extendedattributes object
// Read from the database
Public void deserializeextendedattributes (byte [] serializedextendedattributes)
{

If (serializedextendedattributes. Length = 0)
Return;
Try
{

Binaryformatter = new binaryformatter ();
Memorystream MS = new memorystream ();

// Stream the byte array to the memory
//
Ms. Write (serializedextendedattributes, 0, serializedextendedattributes. Length );

// Place the memory stream to the initial position
//
Ms. Position = 0;

// Deserialize the object into a namevaluecollection object and create a copy that is exactly the same as the original object
//
Extendedattributes = (namevaluecollection) binaryformatter. deserialize (MS );

Ms. Close ();
}
Catch {}

}

Essentially, the serialization mechanism is to convert the class value into a general (that is, continuous) byte stream, then you can save the stream to a field in the database (in the forums_userprofile table of the database, there is a field "stringnamevalues varbinary (7500 )"). When the object is deserialized, a copy identical to the original object is created.

Related Article

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.