In the. NET Framework, the software provides the serialization function through system. runtime. serialization and system. xml. serialization. here we can see that Microsoft has acknowledged the need for persistent data. System. runtime. serialization provides a framework for developing custom serialization solutions. The system. xml. serialization namespace provides the basic functions of persistence objects in XML. Let's take a closer look at how to use this method.
XMLGeneral requirements
XML serialization refers to the process of converting the public property and field of an object to a serialization format (XML here) for storage or transmission. Deserialization is the process of re-creating an object from the XML output according to the original state of the object. Therefore, you can think of serialization as a way to save the object state to a stream or a buffer.
Two common parts of a sequence are data persistence and data exchange. Persistence refers to the process of saving data between user sessions. When the application is closed, the data is stored (serialized) and the data is reloaded (deserialized) when the user returns ). Data exchange is the process of providing data to other systems. Serialization and XML can be used to easily implement data exchange.
The data in the object is described as class, field, property, primitive type, and array in the programming statement structure ), even using system. XML. xmlelement or system. XML. XML embedded in xmlattribute format.
The center class of the system. xml. serialization namespace is xmlserializer. It also contains classes for processing XML and soap, but xmlserializer is our focus.
Xmlserializer
The xmlserializer class allows you to serialize data to XML documents and deserialize objects from XML documents. It allows you to control how objects are encoded into XML. Its Class constructor accepts object types as parameters for serialization. The following C # code shows how to use the builder:
Xmlserializer SER = new xmlserializer (typeof (objecttoserialize ));
The corresponding VB. NET code is as follows:
Dim SER as new xmlserializer (GetType (objecttoserialize ))
The actual serialization process is completed by the serialize method of the xmlserializer class. This method has six signatures that allow you to use textwriter, stream, and xmlwriter objects during serialization. The following sample code shows how to use this method. In this example, an object is serialized to a file on the local disk drive. The following lists the code for using classes, and then the actual serialization code:
Using system;
Namespace builderserialization {
Public Class address {
Public Address (){}
Public String address1;
Public String address2;
Public String city;
Public String state;
Public String zip;
Public String country;
}}
Using system;
Namespace builderserialization {
Public class author {
Public author (){}
Public String firstname;
Public String middlename;
Public String lastname;
Public String title;
Public String gender;
Public Address addressobject;
}}
Namespace builderserialization {
Public class book {
Public book (){}
Public String title;
Public author authorobject;
Public String ISBN;
Public double retailprice;
Public String publisher;
}}
Using system;
Using system. xml. serialization;
Using system. IO;
Namespace builderserialization {
Class testclass {
Static void main (string [] ARGs ){
Book bookobject = New Book ();
Xmlserializer SER = new xmlserializer (typeof (book ));
Textwriter writer = new streamwriter ("booktest. xml ");
Bookobject. Title = "Practical Lotus script ";
Bookobject. ISBN = "1884777767 ";
Bookobject. Publisher = "Manning publications ";
Bookobject. retailprice = 43.95;
Bookobject. authorobject = new author ();
Bookobject. authorobject. firstname = "Tony ";
Bookobject. authorobject. lastname = "Patton ";
Bookobject. authorobject. Gender = "male ";
Bookobject. authorobject. addressobject = new address ();
Bookobject. authorobject. addressobject. address1 = "1 Main Street ";
Bookobject. authorobject. addressobject. City = "Anywhere ";
Bookobject. authorobject. addressobject. State = "Ky ";
Bookobject. authorobject. addressobject. Zip = "40000 ";
Bookobject. authorobject. addressobject. Country = "USA ";
Ser. serialize (writer, bookobject );
Writer. Close ();
}}}
This Code uses three objects. These three objects are combined into one object, so the serialization process generates an XML document. Below is the XML created by the sample code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Book xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">
<Title> practical LotusScript </title>
<Authorobject>
<Firstname> Tony </firstname>
<Lastname> Patton </lastname>
<Gender> male </gender>
<Addressobject>
<Address1> 1 Main Street </address1>
<City> anywhere </city>
State> Ky </State>
<Zip> 40000 </zip>
<Country> USA </country>
</Addressobject>
</Authorobject>
<ISBN> 1884777767 </ISBN>
<Retailprice> 43.95 </retailprice>
<Publisher> Manning publications </publisher>
</Book>
Note that the serialization process processes object data nesting. This process places data in a useful format so that data can be easily loaded (deserialized) when needed or exchanged with other systems ). When exchanging data, other systems (if they do not know the XML format) Need to Know the XML format. This can be provided through an XML schema file .. Net Framework contains a schema generation tool xsd.exe, which can be used to generate a schema file for the XML generated through serialization.
The following is the VB. NET code in the previous example:
Public Class address
Public address1 as string
Public address2 as string
Public City as string
Public state as string
Public zip as string
Public country as string
End Class
Public class author
Public firstname as string
Public middlename as string
Public lastname as string
Public title as string
Public Gender as string
Public addressobject as address
End Class
Public class Book
Public authorobject as author
Public title as string
Public ISBN as string
Public retailprice as double
Public publisher as string
End Class
Imports system. xml. serialization
Imports system. Io
Module module1
Sub main ()
Dim bookobject as new book
Dim SER as new xmlserializer (GetType (book ))
Dim writer as new streamwriter ("booktest. xml ")
With bookobject
. Title = "Practical LotusScript"
. ISBN = "1884777767"
. Publisher = "Manning publications"
. Retailprice = 43.95
. Authorobject = new author
. Authorobject. firstname = "Tony"
. Authorobject. lastname = "Patton"
. Authorobject. Gender = "male"
. Authorobject. addressobject = new address
. Authorobject. addressobject. address1 = "1 Main Street"
. Authorobject. addressobject. City = "Anywhere"
. Authorobject. addressobject. State = "Ky"
. Authorobject. addressobject. Zip = "40000"
. Authorobject. addressobject. Country = "USA"
End
Ser. serialize (writer, bookobject)
Writer. Close ()
End sub
End Module
Direction of provision
The serialization process outputs standard XML, where data members serve as elements of the output XML, but you can customize the XML output by adding attributes to the class code. This allows you to create attributes instead of XML elements, or tell the system to simply ignore an element. The VB. net book class is rewritten as an example for you:
Public class Book
Public authorobject as author
Public title as string
<System. xml. serialization. xmlattribute ()> _
Public ISBN as string
<System. xml. serialization. xmlignoreattribute ()> _
Public retailprice as double
Public publisher as string
End Class
This code tells the system to regard ISBN as an XML Attribute when generating XML and ignore retailprice members. The following XML reflects the results of these changes:
<? XML version = "1.0" encoding = "UTF-8"?>
<Book xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" ISBN = "1884777767">
<Authorobject>
<Firstname> Tony </firstname>
<Lastname> Patton </lastname>
<Gender> male </gender>
<Addressobject>
<Address1> 1 Main Street </address1>
<City> anywhere </city>
<State> Ky </State>
<Zip> 40000 </zip>
<Country> USA </country>
</Addressobject>
</Authorobject>
<Title> practical LotusScript </title>
<Publisher> Manning publications </publisher>
</Book>
The same example written in C:
Public class book {
Public book (){}
Public String title;
Public author authorobject;
[System. xml. serialization. xmlattribute ()]
Public String ISBN;
[System. xml. serialization. xmlignoreattribute ()]
Public double retailprice;
Public String publisher;
}
Only the available attributes are provided here. Refer to the. NET document to view a complete list of these identifiers.
Deserialization
Deserialization of data is a simple process that can be achieved through the deserialize method of the xmlserializer class. The following VB. NET code snippet deserializes the output in the previous example.
Dim bookobject as new book
Dim SER as new xmlserializer (GetType (book ))
Dim FS as new system. Io. filestream ("booktest. xml", filemode. open)
Dim reader as new system. xml. xmltextreader (FS)
Bookobject = ctype (Ser. deserialize (Reader), book)
This code puts data in the memory for use when needed. Below is the C # code version:
Xmlserializer SER = new xmlserializer (typeof (book ));
System. Io. filestream FS = new system. Io. filestream ("booktest. xml ",
Filemode. Open );
System. xml. xmltextreader reader = new system. xml. xmltextreader (FS );
Book bookobject = (book) (Ser. deserialize (Reader ));