asp.net XML serialization and deserialization 1th/2 page _ Practical Tips

Source: Internet
Author: User
Tags soap net xml object serialization serialization
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
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.