Serialized XML (1)

zhaozj2021-02-16  51

.NET Framework provides a wide range of support for creating distributed applications and services. When a distributed system is created, a question that must be solved is how to get some of the object from the system to another. In order to meet the needs of this mechanism, the .NET Framework implemented an XML-based serialization mechanism. This can serialize an object to an XML format so that all public domains and properties will be written in XML, and then this XML can be sent to anywhere, and the receiving party can parallelize the XML data, thus Reconstructive objects in memory. This makes it easy to solve the problem of object exchange in a distributed system.

The serialization mechanism is very strong, so it can be flexible to control the object serialized to XML format (such as: You can use XSD mode to define the XML document structure generated during serialization).

In addition to serialization of XML, there are Binaryformatter and SOAPFormatter formatted classes.

S binaryformatter serializes the object to a binary format. This can be transmitted to the remote application very efficiently.

S SOAPFormatter serializes the object to the Simple Object Access Protocol (SOAP) format.

SOAP is an XML-based definition method calling parameters and return value symbols. When the object parameters are passed to a web service method or return to the object from a web service method, SOAPFormatter uses the object to SOAP format for transmission, .NET provides us with a simple way to store an object status.

In summary, .NET serialization mechanism can transmit object data between the system at the system layers in the N-layer application system.

To perform XML serialization of the object, first define a class:

Public Class Book

Public bookid as integer

Public bookname as string, PUBLIC BOOKNAME AS STRING

END CLASS

This class must be a public class, if you try to serialize a non-public class, XMLSerializer will throw an exception at runtime. Only serialize only those public members in the inside of the class, and non-public members will not be serially connected to XML. If you have to serialize non-public members, you can use binaryformatter or soapformatter.

'Instantiate Book Class

DIM Book As New Book

Book.bookid = 1

Book.bookname = "Mathematics"

'Creating an XML serializer

DIM Serializer as new xml.serialization.xmlserializer (Gettype (Book)

DIM Writer as system.io.streamwriter = new system.io.streamwriter ("e: /serializer/book.xml)

'For serialization

Serializer.Serialize (Writer, Book)

Writer.close ()

In this way, the object can be serially connected to an XML file, the format is:

1

Mathematics

If there are many classes that need XML serialization, you can create an array of XMLSERIALIZER objects.

Public Class Book

Public bookid as integer

Public bookname as string, PUBLIC BOOKNAME AS STRING

END CLASS

Public Class Student

Public StudentID AS INTEGER

Public studentname as string

END CLASS

Public Class SHOP

Public SHOPID AS INTEGER

Public SHOPNAME AS STRING

END CLASS

DIM TYPES (3) as Type

Types (0) = getType (BOOK)

Types (1) = getType (student)

Types (2) = getType (SHOP)

DIM Serializers (3) as Xml.Serialization.xmlSerializer

Serializers = Xml.Serialization.xmlSerializer.FromTypes (Types)

DIM Writer as system.io.streamwriter = new system.io.streamwriter ("e: /serializer/book.xml)

The fromTypes () method returns an array of XMLSerializer objects. The generated XML document element is mapped to class name, and its sub-elements are mapped to the public domain of the class, and there are also XSD namespaces.

Restore the serialized object to XML, then the XML data object is required, call the DeserialIze method of the XMLSerializer object, and then use the object as usual.

DIM Serializer as new xml.serialization.xmlserializer (Gettype (Book)

Dim reader as system.io.streamreader = new system.io.streamReader ("e: /serializer/book.xml)

DIM A as book = ctype (Serializer.Deserialize (Reader), BOOK

Reader.close ()

Console.writeline ("Book ID:" & a.bookid)

Console.writeline ("Book Name:" & a.bookname)

Console.WriteLine ("Print Enter to Exit")

Console.readline ()

The operation is as follows:

转载请注明原文地址:https://www.9cbs.com/read-23483.html

New Post(0)