As we have talked about how to use the various features in the System.xml.Serialization namespace to mark the class to serialize, and then say how to use XMLSerializer to perform serialization of objects. If you miss the object serialization, you can learn from the following two links:
●
Realize the sequence of sequence in .NET
●
Object Serialization: Name the SYSTEM.XML.SERIALIZATION
XMLSerializer's serialize provides 6 heavy load methods, actually belonging to three types. It allows sequences to be saved to TextWriter, Stream, XMLWRITER objects.
Just in the object serialization: Using the System.xml.Serialization namespace, the advantage of using XMLTextWriter is that you can explicitly specify eNCoding in serialized XML file declarations, and if you are using textwriter or stream, then The default encoding mode will be pressed by .NET (the forefront has already been said, UTF8 can be used, but this is just my guess).
If you want to save the serialized XML document as a local file, use XMLTextWriter is the best choice. However, if you want to transfer serialized XML documents directly, use Stream best. .NET provides a variety of STREAM inheritance class for STREAM processing in different occasions. To directly transmit serialized XML documents, MemoryStream is a good choice.
DIM
MEM
AS
New
System.io.MemoryStream
DIM
Serializer
AS
New
XMLSERIALIZER (
Gettype
(Order)) Serializer.Serialize (MEM, O, NS)
If you want to explicitly specify eNCoding in serialized XML file declaration, you can still use XMLTextWriter, MemoryStream is not less.
DIM
MEM
AS
New
System.io.MemoryStream
DIM
Writer
AS
New
XmlTextWriter (MEM, Encoding.utf8)
DIM
Serializer
AS
New
XMLSERIALIZER (
Gettype
(ORDER)) Serializer.Serialize (Writer, O, NS)
In this way, it can be transmitted directly using the MemoryStream's ToARRAY method in a BYTE array, or encoding.utf8. GetString (Mem. Toarray) converts the BYTE array into a String type.
Everything seems to be smooth, but I encountered a problem when I use XMLDocument to verify serialized XML documents. Or use the previous example, when I use the following code, I always generate the data invalid on the root level of XMLEXCEPTION exceptions. "
DIM
DOC
AS
New
XMLDocument Doc.LoadXml (Encoding.utf8.Getstring (Mem.Toarray))
The result of printing with console.writeline is the following look, very complete XML document, just a few "?":
?
XML Version = "1.0" encoding = "UTF-8"
?>
<
ORDER
id
= "123456">
> 2005-04-05 ORDERDATE > < Items > < Item > < Name > Object sequence Name > Item > Items > ORDER > I originally thought this? It is included in the serialized XML document, but the startwith method that uses String can't find this? It seems that it is not part of the XML document. When using TRIM, everything is ok ,? Nothing, no abnormality is not :) DIM DOC AS New XmlDocument doc.loadxml (encoding.utf8.getstring (mem.toarray). TRIM ())