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.