Serialized XML (2)

zhaozj2021-02-16  51

The serialization of XML is known from the serialization XML (1). It is such a process: get an object, convert the object to an XML document, and save it for use, or transfer it to the network for use. In .NET, XML Serialization works by checking whether the attribute of the class is logo Read / Write. As long as there is one of the properties, the value of this class will be removed and inserted into an XML document. De-serialization is an opposite process.

After serving a subject to XML, then confine, so that it is restored to the original object, there may be an unexpected XML node in parallel XML.

If these unexpected XML nodes are not processed, XMLSerializer will ignore these unexpected nodes. If you want to handle these accident nodes, you can use the XMLSerializer event to process:

¨ unknownnode

¨ unknowneElement

¨ UnknownAttribute

¨ UnreferenceDObject

Now change the format of book.xml to:

1

Mathematics

Some Text

We have added some unexpected nodes, attributes, and then execute the parallelization process:

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

AddHandler Serializer.unknownAttribute, Addressof MyunknownAttribute

AddHandler Serializer.unkNowneElement, Addressof MyunkNowneElement

AddHandler Serializer.unknownNode, Addressof MyunknownNode

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

Dim a as bookshop = ctype (Serializer.Deserialize (Reader), Bookshop

Reader.close ()

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

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

Console.writeline ("Print Enter to EXIT ...........")

Console.readline ()

We also added an accident node, the property processing code, and the execution result is:

This way we can see those unexpected nodes and properties.

In addition, we can also flexibly control the format of serial XML, so that it can be easily exchanged with external applications. We can join the .NET Framework property in the class to control the format of the serialized XML. Imports system.xml

Imports System.xml.Serialization

_ _

Public Class Book

Public BookID AS Integer

_

Public bookname as string, PUBLIC BOOKNAME AS STRING

_ _

Public Test_Text As String

_ _

Public IgnoreText As String

'IgnoreText will not be serialized

END CLASS

Module Serialization

_ _

Public Sub Main ()

DIM Book As New Book

Book.bookid = 1

Book.bookname = "Mathematics"

Book.test_text = "Some text"

Book.ignoreText = "IgnoreText"

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

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

Serializer.Serialize (Writer, Book)

Writer.close ()

End Sub

End module

The generated XML format is:

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


New Post(0)