Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Xml serialization and deserialization of XmlSerializer object

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the XmlSerializer object how to achieve Xml serialization and deserialization, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Why serialize and deserialize?

When a .net program executes, objects reside in memory; objects in memory need to be passed to other systems for use; or they need to be saved when shutting down so that the next time the program is started again, serialization and deserialization are needed.

Scope: this article only introduces xml serialization, but serialization can be binary serialization or other format serialization.

Look at the simplest piece of Xml serialization code

The copy code is as follows:

Class Program

{

Static void Main (string [] args)

{

Int I = 10

/ / declare Xml serialization object instance serializer

XmlSerializer serializer = new XmlSerializer (typeof (int))

/ / perform serialization and output the serialization result to the console

Serializer.Serialize (Console.Out, I)

Console.Read ()

}

}

The above code serializes int I and outputs the serialized result to the console, which is as follows

The copy code is as follows:

ten

The above serialized xml can be deserialized as follows

The copy code is as follows:

Static void Main (string [] args)

{

Using (StringReader rdr = new StringReader (@ "

10 "))

{

/ / declare serialized object instance serializer

XmlSerializer serializer = new XmlSerializer (typeof (int))

/ / deserialize and assign the deserialization result value to the variable I

Int I = (int) serializer.Deserialize (rdr)

/ / output deserialization result

Console.WriteLine ("I =" + I)

Console.Read ()

}

}

The above code illustrates the process of xml serialization and deserialization in the simplest way. The .net system class library has done a lot of work for us, serialization and deserialization are very simple. But in reality, business requirements are often more complex, it is not possible to simply serialize an int variable, showing that we need to control the serialization of complex types.

Xml serialization of custom objects:

The System.Xml.Serialization namespace has a series of feature classes that control the serialization of complex types. For example, XmlElementAttribute, XmlAttributeAttribute, XmlArrayAttribute, XmlArrayItemAttribute, XmlRootAttribute and so on.

Looking at a small example, there is a custom class Cat,Cat that has three properties called Color,Saying,Speed.

The copy code is as follows:

Namespace UseXmlSerialization

{

Class Program

{

Static void Main (string [] args)

{

/ / declare a cat object

Var c = new Cat {Color = "White", Speed = 10, Saying = "White or black, so long as the cat can catch mice, it is a good cat"}

/ / serialize this object

XmlSerializer serializer = new XmlSerializer (typeof (Cat))

/ / output object serialization to the console

Serializer.Serialize (Console.Out, c)

Console.Read ()

}

}

[XmlRoot ("cat")]

Public class Cat

{

/ / define the serialization of the Color attribute to the attribute of the cat node

[XmlAttribute ("color")]

Public string Color {get; set;}

/ / No serialization of Speed attribute is required

[XmlIgnore]

Public int Speed {get; set;}

/ / sets the Saying attribute to serialize to XML child elements

[XmlElement ("saying")]

Public string Saying {get; set;}

}

}

You can use XmlElement to specify that attributes are serialized into child nodes (by default, they are serialized to child nodes); or you can use the XmlAttribute attribute to serialize attributes that are serialized as Xml nodes; you can also use XmlIgnore attribute decorations to require serializers not to serialize decorated attributes.

Xml serialization of an array of objects:

The Xml serialization of an array requires the use of XmlArrayAttribute and XmlArrayItemAttribute;XmlArrayAttribute to specify the Xml node name of the array element, and XmlArrayItemAttribute specifies the Xml node name of the array element.

The following code example:

The copy code is as follows:

Using System

Using System.Collections.Generic

Using System.Linq

Using System.Text

Using System.Xml.Serialization

Namespace UseXmlSerialization

{

Class Program

{

Static void Main (string [] args)

{

/ / declare a cat object

Var cWhite = new Cat {Color = "White", Speed = 10, Saying = "White or black, so long as the cat can catch mice, it is a good cat"}

Var cBlack = new Cat {Color = "Black", Speed = 10, Saying = "White or black, so long as the cat can catch mice, it is a good cat"}

CatCollection cc = new CatCollection {Cats = new Cat [] {cWhite,cBlack}}

/ / serialize this object

XmlSerializer serializer = new XmlSerializer (typeof (CatCollection))

/ / output object serialization to the console

Serializer.Serialize (Console.Out, cc)

Console.Read ()

}

}

[XmlRoot ("cats")]

Public class CatCollection

{

[XmlArray ("items"), XmlArrayItem ("item")]

Public Cat [] Cats {get; set;}

}

[XmlRoot ("cat")]

Public class Cat

{

/ / define the serialization of the Color attribute to the attribute of the cat node

[XmlAttribute ("color")]

Public string Color {get; set;}

/ / No serialization of Speed attribute is required

[XmlIgnore]

Public int Speed {get; set;}

/ / sets the Saying attribute to serialize to XML child elements

[XmlElement ("saying")]

Public string Saying {get; set;}

}

}

The above code will output:

The copy code is as follows:

White or black, so long as the cat can catch mice, it is a good

Cat

White or black, so long as the cat can catch mice, it is a good

Cat

XmlSerializer memory leak problem:

If you take a closer look at msdn, you can see that there is indeed a leak. Msdn explains as follows:

Dynamically generated assembly

To improve performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types. This infrastructure will find and reuse these assemblies. This behavior occurs only when the following constructor is used:

XmlSerializer (Type)

XmlSerializer.XmlSerializer (Type, String)

If any other constructor is used, multiple versions of the same assembly will be generated and will never be uninstalled, resulting in memory leaks and performance degradation. The simplest solution is to use one of the two constructors mentioned earlier. Otherwise, the assembly must be cached in Hashtable, as shown in the following example.

That is to say, when we are using XmlSerializer serialization, it is best to use the following two constructors when initializing XmlSerializer objects, otherwise it will cause a memory leak.

XmlSerializer (Type)

XmlSerializer.XmlSerializer (Type, String)

Thank you for reading this article carefully. I hope the article "how to serialize and deserialize XmlSerializer objects" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report