Application of serialization and deserialization in. Net in asp.net forums

Source: Internet
Author: User
Tags modify net serialization serialization
asp.net

In forums, some of the content is not fixed, such as user data, in addition to some basic information, there may be some other information, such as MSN, Personal homepage, signature file, etc., generally for such a property corresponds to a field in the database. But if later we need to add some attributes, such as QQ number, blog address, and so on, if you use this method to increase the data table field, it will frequently modify the structure of database tables, stored procedures, database access procedures.

Perhaps you have encountered similar problems, see how forums in the use of. NET serialization and deserialization to solve:
For example, I need to add the QQ number to the user profile, so I just need to add an attribute to the users class
Public String Qqim
{
get {return Getextendedattribute ("Qqim");}
set {Setextendedattribute ("Qqim", value);}
}
You do not need to modify the database table structure, you do not need to modify the stored procedures, even the database access programs do not need to move.

The main code for its implementation:

First create a new NameValueCollection object in the user class and save the extended properties in the NameValueCollection object
NameValueCollection extendedattributes = new NameValueCollection ();

Take a record from the NameValueCollection collection
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 an extended property in NameValueCollection
public void Setextendedattribute (string name, String value)
{
Extendedattributes[name] = value;
}

Serializes the ExtendedAttributes object (the NameValueCollection object previously defined to hold all user-extended information) into a memory stream
Can be used to save in a database
Public byte[] Serializeextendedattributes ()
{

Serializing an Object
BinaryFormatter BinaryFormatter = new BinaryFormatter ();

Creates a memory stream, which is serialized and saved in the
MemoryStream ms = new MemoryStream ();
Byte[] B;

Serializes the ExtendedAttributes object (which holds all of the user's extended information) into the 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;
}

Deserializing the contents of a ExtendedAttributes object
Read from the database.
public void Deserializeextendedattributes (byte[] serializedextendedattributes)
{

if (serializedextendedattributes.length = 0)
Return
Try
{

BinaryFormatter BinaryFormatter = new BinaryFormatter ();
MemoryStream ms = new MemoryStream ();

Converts a byte array to a memory stream
//
Ms. Write (serializedextendedattributes, 0, serializedextendedattributes.length);

Position the memory stream to the very beginning
//
Ms. Position = 0;

Deserializes into a NameValueCollection object, creating exactly the same copy as the original object
//
ExtendedAttributes = (NameValueCollection) binaryformatter.deserialize (MS);

Ms. Close ();
}
Catch {}

}
Essentially, the serialization mechanism converts the value of a class to a generic (that is, continuous) byte stream, which can then be saved to a field in the database (in the Database Forums_userprofile table has a field "stringnamevalues varbinary" ( 7500) "). When the read process deserializes an object, it creates a copy that is exactly the same as the original object.

Note that generally such attributes are not retrieved in the database, and that these properties are serialized.

For more information, check out MSDN and asp.net forums source



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.