In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use XMLSerializer to serialize objects to XML", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "how to use XMLSerializer to serialize objects to XML" this article.
The charm of XML
Serialization XML refers to the process of saving the common fields and properties of an object to a serial format (in this case, XML format) for convenient storage or transmission. Non-serialization is the process of restoring an object from a serial XML state to its original state using serial state information. Therefore, serialization can be seen as a way to save the state of an object to a stream or buffer.
The purpose of serialization is data storage and data conversion. Data storage refers to saving data during a user session. When the application is closed, the data is saved (serialized), and when the user comes back, the data is reloaded (deserialized). Data conversion refers to the transformation of data into a format that can be recognized by another system. Using serialization and XML, data conversion can be done easily.
The data in an object can be a class, method, property, private type, array, or even an embedded XML in a System.Xml.XmlElement or System.Xml.XmlAttribute object.
The key class in the System.Xml.Serialization namespace is XmlSerializer. Of course, other aspects of XML and other classes related to SOAP are included in this namespace, but our focus is on the XmlSerializer class.
XmlSerializer
The XmlSerializer class provides methods to serialize objects into XML files and to deserialize XML documents into objects. It also allows the user to specify how the object is converted to XML. You can take the type of the object to be serialized as an argument to the class constructor. The following C # code illustrates the use of the constructor.
XmlSerializer ser = new XmlSerializer (typeof (objectToSerialize))
Here is the equivalent VB.NET code:
Dim ser As New XmlSerializer (GetType (objectToSerialize))
The actual serialization process is implemented in the Serialize method of the XmlSerializer class. This method allows TextWriter, Stream, and XmlWriter objects to be called during serialization. The following example code shows how to call this method. In this example, an object is serialized to a file on the local disk. The example starts with a class declaration, followed by serialized source code.
Using System;namespace BuilderSerialization {public class Address {public Address () {} public string Address1;public string Address2;public string City;public string State;public string Zip;public string Country;}} using System;namespace BuilderSerialization {public class Author {public Author () {} public string FirstName;public string MiddleName;public string LastName;public string Title;public string Gender;public Address AddressObject;}} namespace BuilderSerialization {public class Book {public Book () {} public string Title;public Author AuthorObject;public string ISBN;public double RetailPRice;public string Publisher;}} using System Using System.Xml.Serialization;using System.IO;namespace BuilderSerialization {class TestClass {static void Main (string [] args) {Book BookObject = new Book (); XmlSerializer ser = new XmlSerializer (typeof (Book)); TextWriter writer = new StreamWriter ("booktest.xml"); BookObject.Title = "Practical LotusScript"; BookObject.ISBN = "1884777767"; BookObject.Publisher = "Manning Publications"; BookObject.RetailPrice = 43.95 bot. AuthorObject = new Author (); BookObject.AuthorObject.FirstName = "Tony"; BookObject.AuthorObject.LastName = "Patton" BookObject.AuthorObject.Gender = "Male"; BookObject.AuthorObject.AddressObject = new Address (); BookObject.AuthorObject.AddressObject.Address1 = "1 Main Street"; BookObject.AuthorObject.AddressObject.City = "Anywhere"; BookObject.AuthorObject.AddressObject.State = "KY"; BookObject.AuthorObject.AddressObject.Zip = "40000"; BookObject.AuthorObject.AddressObject.Country = "USA"; ser.Serialize (writer, BookObject); writer.Close ();}}}
The above code turns three objects into one object, so a XML file is generated during serialization. The following is the XML document generated by the example program:
Practical LotusScriptTonyPattonMale1 Main StreetAnywhereKY40000USA1884777767 43.95Manning Publications
Note that the serialization process can also handle nesting of object data. The data is converted into a recognizable format to facilitate data overloading (non-serialization) and data transmission to another system. During the data transfer process, the receiver system needs to know the format of the XML file (if it does not know it in advance). Therefore, you need to provide a XML schema file. The XSD.exe tool in the .NET Framework can generate an schema file for serialized XML.
Here is the sample code written in VB.NET:
Public Class AddressPublic Address1 As StringPublic Address2 As StringPublic City As StringPublic State As StringPublic Zip As StringPublic Country As StringEnd ClassPublic Class AuthorPublic FirstName As StringPublic MiddleName As StringPublic LastName As StringPublic Title As StringPublic Gender As StringPublic AddressObject As AddressEnd ClassPublic Class BookPublic AuthorObject As AuthorPublic Title As StringPublic ISBN As StringPublic RetailPrice As DoublePublic Publisher As StringEnd ClassImports System.Xml.SerializationImports System.IOModule Module1Sub Main () Dim BookObject As New BookDim ser As New XmlSerializer (GetType (Book)) Dim writer As New StreamWriter ("booktest.xml") With BookObject.Title = "Practical LotusScript" .ISBN = "1884777767" .publisher = "Manning" Publications ".RetailPrice = 43.95.AuthorObject = New Author.AuthorObject.FirstName =" Tony ".AuthorObject.LastName =" Patton ".AuthorObject.AddressObject = New Address.AuthorObject.AddressObject.Address1 =" 1 Main Street ".AuthorObject.AddressObject.City =" Anywhere ".AuthorObject.AddressObject.State =" KY ".AuthorObject.AddressObject.Zip =" 40000 ".AuthorObject.AddressObjec t.Country =" USA "End Withser.Serialize (writer) BookObject) writer.Close () End SubEnd Module
Control output
The serialization process generates a standard XML file, and the data members are converted into XML elements. However, not all data members become elements, and you can control the output of the XML file by adding some tags to the class code. In this way, data members can be transformed into XML attributes rather than elements, or they can be simply ignored. The following example is a modified VB.NET code of the book class.
Public Class BookPublic AuthorObject As AuthorPublic Title As String _ Public ISBN As String _ Public RetailPrice As DoublePublic Publisher As StringEnd Class
This code tells the system to use the class member ISBN as the XML attribute when generating the XML file, while ignoring the RetailPrice member. This change can be seen in the generated XML file:
TonyPattonMale1 Main StreetAnywhereKY40000USAPractical LotusScriptManning Publications
The following is the corresponding C# code:
Public class Book {public Book () {} public string Title;public Author AuthorObject; [System.Xml.Serialization.XmlAttribute ()] public string ISBN; [System.Xml.Serialization.XmlIgnoreAttribute ()] public double RetailPrice;public string Publisher;}
Only two kinds of markings are mentioned above. Please consult the .NET documentation for complete markup symbols.
Non-serialization
Non-serialized data can be easily implemented by calling the Deserialize method of the XmlSerializer class. The following VB.NET program snippet completes the deserialization of the XML document above:
Dim BookObject As New BookDim ser As New XmlSerializer (GetType (Book)) Dim fs As New System.IO.FileStream ("booktest.xml", FileMode.Open) Dim reader As New System.XML.XmlTextReader (fs) BookObject = CType (ser.Deserialize (reader), Book) this program puts the result data in memory for backup. Here is the equivalent C # code: XmlSerializer ser = new XmlSerializer (typeof (Book)); System.IO.FileStreamfs = new System.IO.FileStream ("booktest.xml", FileMode.Open); System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader (fs); Book BookObject = (Book) (ser.Deserialize (reader)); above is all the content of the article "how to serialize objects to XML using XMLSerializer". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.