Object Serialization: Name the SYSTEM.XML.SERIALIZATION

xiaoxiao2021-03-05  25

To use .NET's serialization, you must add a reference to System.xml in the solution and introduce System.xml.Serialization namespaces in the class file. This allows the various characteristics required to use serialization in the file.

Imports

System.xml.Serialization

If you miss the XML Serialization, please refer to yourself:

Realize the sequence of sequence in .NET

XML Version = "1.0" encoding = "UTF-8"

?>

<

ORDER

id

= "123456">

>

2005-04-05

ORDERDATE

>

<

Items

>

<

Item

>

<

Name

>

Object sequence

Name

>

Item

>

Items

>

ORDER

>

The above example contains typical XML common elements: XML declaration, XML root node, XML node, XML property, XML collection. In addition to the XML declaration, there are corresponding features in .NET to define these elements. These features include: XMLRootttribute, XmLTyPeattribute, XmlattributeAttribute, XmlarrayAttribute, and XmlarrayItemAttribute. In addition, there are two commonly used features, XmlignoreAttribute is used to mark a portion that needs to be ignored when the object serialization is used, and XMLinCludeAttribute is used to mark the type that needs to be included when generating XML Schema.

If no characteristics are explicitly tagged, the default class features the characteristics of XMLTyPeattribute, class members to XMLELEmentAttribute, and names for class or class members. E.g:

Public

Class ORDER

Class Order Public ID AS STRING PUBLIC ORDERDATE AS STRING END CLASS

If you do not do any characteristic tag, use the following code serialization:

DIM

o

AS

New

ORDER

WITH

O.ID

=

123456

.Ordad

=

Date

.Now.toshortdateString

End

WITH

DIM

Writer

AS

New

XMLTextWriter

"

ABC.XML

"

Encoding.utf8)

DIM

Serializer

AS

New

XMLSERIALIZER (

Gettype

(Order)) Writer.Formatting

=

Formatting.indented Serializer.Serialize (Writer, O)

The serialized XML is:

XML Version = "1.0" encoding = "UTF-8"

?>

<

ORDER

XMLns: XSD

= "http://www.w3.org/2001/xmlschema"

XMLns: XSI

= "http://www.w3.org/2001/xmlschema-instance"

>

<

Id

>

123456

Id

>

<

ORDERDATE

>

2005-4-11

ORDERDATE

>

ORDER

>

It can be seen that corresponds to the Order class, and and respectively correspond to the field ID and ORDERDATE in the Order class. In addition, there is more an XML declaration and two XML namespaces.

The XML declaration is automatically added .NET, but encoding is specified in XmlTextWriter, if you do not specify eNCoding, then the XML declaration is only . I am using .NET 1.1, only support XML 1.0 version is supported. Also, if encoding is not specified, the default encoding may also be UTF8 (not found).

.NET defaults to the ORDER class adds two W3C namespaces for Xmlschema and Xmlschema-Instance. This namespace can also specify themselves, the method is another serialize method for using XMLSerializer.

DIM

ns

AS

New

XMLSerializerNamespaces ns.add (

""

,

""

Writer.Formatting

=

Formatting.indented Serializer.Serialize (Writer, O, NS)

To set the class sequence to an XML node:

<

XMLTYPE

"

ORDER

"

)

>

_

Public

Class ORDER

Class ORDER 'Any Code Here. End Class

To set the class sequence to XML root node:

<

XMLROOT

"

ORDER

"

)

>

_

Public

Class ORDER

Class ORDER 'Any Code Here. End Class

When using XMLRootattribute, XMLTYPEATTRIBUTE in the class, the type in the serialization document is based on XMLRootAttribute:

<

XMLROOT

"

ORDER

"

), XMLTYPE

"

Anotherordername

"

)

>

_

Public

Class ORDER

Class ORDER 'Any Code Here. End Class

To serialize the class member into an XML node:

<

XMlattributeAttribute (

"

id

"

)

>

_

Public

Id

AS

String

To serialize the class member as an XML property:

<

XMlattributeAttribute (

"

id

"

)

>

_

Public

Id

AS

String

To serialize the class member to an XML collection:

<

XMLROOT

"

ORDER

"

), XMLTYPE

"

Anotherordername

"

)

>

_

Public

Class ORDER

Class Order _ Public ID As String Public orderDate As String _ Public Items As New ArrayList End Class <

XMLTYPE

"

ORDERITEM

"

)

>

_

Public

Class ORDERITEM

Class ORDERITEM PUBLIC NAME AS STRING END CLASS

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

New Post(0)