Serialization of XML files

Source: Internet
Author: User

Author:SparonMSN:ZHAOKEYONG@HOTMAIL.COM

 

I haven't written about Dongdong. I have studied it here.. NetObjectXMLSerialization, although not very deep, but still write down, even a milestone in the learning path.

 

InWebformA major challenge in development is Database Calling, althoughDatasetReading data has been greatly improved, but data is always read for a long time every time a website is accessed for the first time. Is there a great way to control it? The answer is yes.. NetThere is a technology called object serialization, which can serialize objects into binary files,XMLFile,SoapFile. In this way, the transmission efficiency of serialized streams is greatly improved.

 

In. NetProvides two types of serialization: binary serialization,XMLAndSoapSerialization. ForWebFor applications, the second method is used most ---XMLAndSoapSerialization.

 

XMLSerialization converts (serializes) the parameters and return values of public fields and attributes of objects or methods to conform to specificXMLArchitecture Definition Language(XSD)DocumentXMLStream.XMLSerialize and generate a strongly-typed class, and convert its public attributes and fields to the sequence format for storage or transmission purposes (in this caseXML).

 

Note:

XMLSerialization does not convert methods, indexers, private fields, or read-only attributes (except for read-only sets ).

 

After understanding the basic theory, let's take a look.XMLHow to serialize objects.

Reading rigid text is boring, or is it used?CodeTo clarify the problem.

 

XMLFile sequence:

SerobjOBJ =New Serobj();

OBJ. Name ="Sparon";

OBJ. Age = 23;

OBJ. Sex =True;

XmlserializerSER =New Xmlserializer(Typeof(Serobj));

StreamFile =New Filestream("Sptxt. xml",Filemode. Create,Fileaccess. Write );

Ser. serialize (file, OBJ );

 

SerobjThe object is as follows:

Public Class Serobj

{

Public StringName;

Public IntAge;

Public BoolSex;// TrueMale; FalseFemale;

PublicSerobj (){}

}

 

In fact, the key steps are only two steps:

XmlserializerSER =New Xmlserializer(Typeof(Serobj));

UseSerobjObject Type to createXmlserializerAnd then useSerializeMethodStreamObject andSerobjObject input together, listing one in sequenceXMLFile.

XMLThe file is as follows:

<?XML Version="1.0"?>

<Serobj Xmlns: xsi="Http://www.w3.org/2001/XMLSchema-instance" Xmlns: XSD="Http://www.w3.org/2001/XMLSchema">

<Name>Sparon</Name>

<Age>23</Age>

<Sex>True</Sex>

</Serobj>

 

 

The deserialization procedure is as follows:

SerobjOBJ =New Serobj();

XmlserializerSER =New Xmlserializer(Typeof(Serobj));

StreamFile =New Filestream("Sptxt. xml",Filemode. Open,Fileaccess. Read );

OBJ = (Serobj) SER. deserialize (File );

 

The difference lies in the last two sentences, and the key is onlyOBJ = (Serobj) SER. deserialize (File );One sentence. This is deserialization.XMLFile and convertSerobjType authorizationOBJ.

 

Note: UseSerializeAndDeserializeSpecify the Command SpaceSystem. xml. serialization

 

Personal Summary

Sequence (Serialize) : The object sequence is a file.

Deserialization (Deserialize): Sets the file sequence as an object.

 

AllCodeDownload:Xmlserialize. CS

 

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.