Asp.net page 1/2 of XML serialization and deserialization

Source: Internet
Author: User

I found some information about XML serialization and deserialization on the Internet. The following is an excerpt:
In. net is called object serialization, which can serialize objects into binary files, XML files, and soap files. In this way, the transmission efficiency of serialized streams is greatly improved.

In. net, two types of serialization are provided: binary serialization, XML serialization, and soap serialization. For Web applications, XML and soap serialization are the second type.

XML serialization converts (serializes) the parameters and return values of common fields and attributes of objects or methods into XML streams that comply with specific XML Schema Definition Language (XSD) documents.

Generate a strongly-typed class in XML serialization, and convert its public attributes and fields to the sequence format for storage or transmission purposes (in this case
XML ).

Note:
1. XML serialization does not convert methods, indexers, private fields, or read-only attributes (except for read-only sets ).
2. To use serialize and deserialize, specify the Command Space System. xml. serialization and using system. Io.
Simple XML serialization application example:

There is a class defined:
C # copyCode
Public class webinfo
{
Public String username;
Public String webname;
Public String weburl;
}
By serialization, We Can serialize it to: XML/html to 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> script home </username>
<Webname> script </webname>
<Weburl> http://www.jb51.net </weburl>
</Webinfo>
The main code is as follows: C # copy the code
Webinfo info = new webinfo ();
Info. Username = "";
Info. webname = "script ";
Info. weburl = "http://www.jb51.net ";

// Use the webinfo class to create an xmlserializer
Xmlserializer SER = new xmlserializer (typeof (webinfo ));

// XML save path. After serialization is successful, you can view the serialized result through the file.
String Path = server. mappath ("webinfo. xml ");

Try
{
// Stream is used to provide a general view of the byte sequence. an XML file is created in the root directory.
Stream file = new filestream (path, filemode. Create, fileaccess. Write );

// Input stream object and info together to serialize an XML file. If this step is not completed, the XML content created is empty.
Ser. serialize (file, Info );

// Release resources
File. Close ();
File. Dispose ();

Response. Write ("serialization successful ");

}
Catch (exception ex)
{
Response. Write (ex. Message );
}
Finally
{

}

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.