


The .Net namespace corresponding to this essay is System.Xml.Serialization; the sample code in the article needs to reference this namespace.
Why do we need to serialize and deserialize?
When the .Net program is executed, the objects reside in the memory; if the objects in the memory need to be passed to other systems for use; or need to be saved when shutting down so that they can be used again when the program is started again, they need to be serialized and deserialized. change.
Scope:This article only introduces xml serialization. In fact, serialization can be binary serialization or serialization in other formats.
Look at the simplest Xml serialization code
class Program { static void Main(string[] args) { int i = 10; //声明Xml序列化对象实例serializer XmlSerializer serializer = new XmlSerializer(typeof(int)); //执行序列化并将序列化结果输出到控制台 serializer.Serialize(Console.Out, i); Console.Read(); } }
The above code serializes int i and outputs the serialization result to the console. The output is as follows
<?xml version="1.0" encoding="gb2312"?> <int>10</int>
You can deserialize the above serialized xml with the following code
static void Main(string[] args) { using (StringReader rdr = new StringReader(@"<?xml version=""1.0"" encoding=""gb2312""?> <int>10</int>")) { //声明序列化对象实例serializer XmlSerializer serializer = new XmlSerializer(typeof(int)); //反序列化,并将反序列化结果值赋给变量i int i = (int)serializer.Deserialize(rdr); //输出反序列化结果 Console.WriteLine("i = " + i); Console.Read(); } }
The above code illustrates the xml serialization and deserialization process in the simplest way. The .Net system class library does it for us A lot of work, serialization and deserialization are very simple. However, in reality, business requirements are often more complex, and it is impossible to simply serialize an int variable. In display, we need to controllably serialize complex types.
Xml serialization of custom objects:
There are a series of feature classes in the System.Xml.Serialization namespace to control the serialization of complex types. For example, XmlElementAttribute, XmlAttributeAttribute, XmlArrayAttribute, XmlArrayItemAttribute, XmlRootAttribute, etc.
Look at a small example. There is a custom class Cat. The Cat class has three attributes: Color, Saying, and Speed.
namespace UseXmlSerialization { class Program { static void Main(string[] args) { //声明一个猫咪对象 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" }; //序列化这个对象 XmlSerializer serializer = new XmlSerializer(typeof(Cat)); //将对象序列化输出到控制台 serializer.Serialize(Console.Out, c); Console.Read(); } } [XmlRoot("cat")] public class Cat { //定义Color属性的序列化为cat节点的属性 [XmlAttribute("color")] public string Color { get; set; } //要求不序列化Speed属性 [XmlIgnore] public int Speed { get; set; } //设置Saying属性序列化为Xml子元素 [XmlElement("saying")] public string Saying { get; set; } } }
You can use XmlElement to specify attributes to be serialized into child nodes (by default, they will be serialized into child nodes); or use the The optimizer does not serialize modified properties.
Xml serialization of object array:
Xml serialization of array requires the use of XmlArrayAttribute and XmlArrayItemAttribute; XmlArrayAttribute specifies the Xml node name of the array element, and XmlArrayItemAttribute specifies the Xml node name of the array element.
The following code example:
/*玉开技术博客 http://www.php.cn/ */ 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) { //声明一个猫咪对象 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} }; //序列化这个对象 XmlSerializer serializer = new XmlSerializer(typeof(CatCollection)); //将对象序列化输出到控制台 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 { //定义Color属性的序列化为cat节点的属性 [XmlAttribute("color")] public string Color { get; set; } //要求不序列化Speed属性 [XmlIgnore] public int Speed { get; set; } //设置Saying属性序列化为Xml子元素 [XmlElement("saying")] public string Saying { get; set; } } }
The above code will output:
<?xml version="1.0" encoding="gb2312"?> <cats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://ww w.w3.org/2001/XMLSchema"> <items> <item color="White"> <saying>White or black, so long as the cat can catch mice, it is a good cat</saying> </item> <item color="Black"> <saying>White or black, so long as the cat can catch mice, it is a good cat</saying> </item> </items> </cats>
situation , msdn description is as follows:
Dynamically generated assembly
To improve performance, the XML serialization infrastructure will dynamically generate assemblies to serialize and deserialize specified types. This infrastructure will find and reuse these assemblies. This behavior only occurs when using the following constructors: XmlSerializer(Type)
XmlSerializer. be unloaded, which will cause memory leaks and reduced performance. The simplest solution is to use one of the two constructors mentioned earlier. Otherwise, the assembly must be cached in a Hashtable, as shown in the following example.
That is to say, we are using XmlSerializer for serialization. When initializing the XmlSerializer object, it is best to use the following two constructors, otherwise it will cause memory leaks.
XmlSerializer(Type)
XmlSerializer.
The above is the detailed content of Sample code analysis of Xml serialization and deserialization of XmlSerializer objects. For more information, please follow other related articles on the PHP Chinese website!

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。详细介绍:1、检查序列化数据,首先检查序列化数据是否有效,并符合PHP的序列化规范,如果数据损坏或者格式错误,可以尝试修复它,或者从备份中恢复正确的数据;2、检查类定义,确保所有在序列化数据中使用的类都存在,并且可以被自动加载,如果类不存在或者无法访问,可以尝试修复类定义等等。

C++函数库序列化和反序列化指南序列化:创建输出流并将其转换为存档格式。将对象序列化到存档中。反序列化:创建输入流并将其从存档格式恢复。从存档中反序列化对象。实战示例:序列化:创建输出流。创建存档对象。创建对象并将其序列化到存档中。反序列化:创建输入流。创建存档对象。创建对象并从存档中反序列化。

随着分布式服务器技术的应用,对象序列化与反序列化这一功能在程序员们的工作中变得愈发平凡。而在Go语言中,也提供了多种实现对象序列化与反序列化的方式,这些方式的使用场景也各不相同。本文将详细介绍Go语言中对象序列化与反序列化的实现方式及其使用方法。一、什么是对象序列化与反序列化?对象序列化和反序列化是指将一个对象数据结构转换成可存储或可传输的形式,以便于后续操

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化序列化和反序列化是在计算机科学中常用的数据处理技巧之一。在PHP中,我们可以使用serialize()和unserialize()函数来实现数据的序列化和反序列化操作。本文将为您详细介绍如何使用这两个函数,并提供相关代码示例。一、什么是序列化和反序列化在计算机编

序列化(Serialization)是将数据结构或对象转换为可传输的数据格式的过程,反序列化(Deserialization)则是将这些数据重新还原成原本的对象或数据结构。在Web开发中,序列化和反序列化技术广泛应用在数据传输、缓存和分布式计算等场景中。PHP作为一种常用的Web后端开发语言,其内置的序列化和反序列化函数是如何实现的?本文将介绍PHP中序列化

接口无法直接序列化,抽象类可以序列化但前提是不包含非静态、非瞬态字段或覆盖writeObject()和readObject()方法,具体实例可通过实现接口的具体类或覆盖writeObject()和readObject()方法的抽象类实现。

序列化是一种将数据结构或对象转换为便于存储、传输或表示的字符串的过程,反之则是将字符串解析为原始的数据结构或对象。在PHP中,我们可以使用serialize()函数将一个变量序列化为字符串,使用unserialize()函数将字符串反序列化为原始的数据结构或对象。本文将重点讲解PHPunserialize()函数的使用及注意事项。一、unserialize

序列化将对象转换为字节序列,反序列化将字节序列还原为对象。序列化用于持久化或传输对象,而反序列化用于重建对象。实战案例中,用户对象序列化写入文件,然后反序列化读出,演示了序列化和反序列化在Java中的实际应用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
