On the Internet to find some information on XML serialization and deserialization, excerpt:
Under. NET there is a technology called object serialization, which can serialize objects into binary files, XML files, and SOAP files, so that the efficiency of transmission using serialized streams is greatly improved.
Two types of serialization are available in. NET: binary serialization, XML, and SOAP serialization. For Web applications, the second ——— XML and SOAP serialization is the most frequently used.
XML serialization converts the arguments and return values of the public fields and properties or methods of an object to an XML stream that conforms to a specific XML Schema definition language (XSD) document.
XML serialization generates strongly typed classes and converts their public properties and fields to sequential format for storage or transport purposes (in this case
XML).
Attention:
1. XML serialization does not convert methods, indexers, private fields, or read-only properties (except for read-only collections).
2, use serialize and deserialize need to indicate the command space system.xml.serialization,using System.IO.
Examples of simple XML serialization applications:
There is a class defined as:
C # Copy Code
public class Webinfo
{
public string UserName;
public string WebName;
public string Weburl;
}
So by serialization we can serialize it to: xml/html copy Code
<?xml version= "1.0"?>
<webinfo xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >
<userName> Cloud-dwelling community </userName>
<webName> Scripts </webName>
<webUrl>http://www.jb51.net</webUrl>
</webinfo>
The main code is as follows: C # copy Code
Webinfo info = new Webinfo ();
Info.username = "cloud-dwelling community";
Info.webname = "Script";
Info.weburl = "Http://www.jb51.net";
Make a XmlSerializer with the Webinfo class
XmlSerializer ser = new XmlSerializer (typeof (Webinfo));
XML save path, after serialization is successful, you can see the result after serialization by viewing the file
String path = Server.MapPath ("Webinfo.xml");
Try
{
Stream is used to provide a general view of the byte sequence, where an XML file is established under the root directory
Stream file = new FileStream (path, FileMode.Create, FileAccess.Write);
The stream object is passed along with info, serializing an XML file, and if not, the established XML content is empty
Ser. Serialize (file, info);
Releasing resources
File. Close ();
File. Dispose ();
Response.Write ("serialization succeeded");
}
catch (Exception ex)
{
Response.Write (ex. message);
}
Finally
{
}
Current 1/2 page
12 Next read the full text