XML(5) serialization to write xml file
Write the content into the xml file through the Xml serializer in .NET. Here we introduce the serialization of list collections.
What is serialization first? What does it do?
Serialization
## Serialization (Serialization) converts the state information of the object into A process in a form that can be stored or transmitted. During serialization, an object writes its current state to temporary or persistent storage. Later, the object can be recreated by reading or deserializing the object's state from the store.
Serialization allows other code to view or modify object instance data that is inaccessible without serialization. Specifically, code that performs serialization requires special permissions: SecurityPermission with the SerializationFormatter flag specified. Under the default policy, via the Internet Downloaded code or intranet code does not grant this permission; only code on the local computer is granted this permission.
Normally, all fields of an object instance will be serialized, which means that the data will be represented as the serialized data of the instance. This way, code that can interpret the format may be able to determine the value of this data without relying on the accessibility of the member. Similarly, deserialization extracts data from the serialized representation and sets object state directly, again regardless of accessibility rules.
Any object that may contain important security data should be made non-serializable if possible. If it must be serializable, try to generate specific fields to hold important data that is not serializable. If this is not possible, you should be aware that the data will be exposed to any code with serialization permissions, and ensure that no malicious code gains that permission.
In summary: Serialization is to convert a complex object into a stream to facilitate our storage and information exchange. I don't know much about security. The main thing is that if some information needs to be kept confidential, define it as non-serializable to prevent others from deserializing it. I am a newbie. If you have any in-depth insights on serialization, please leave your valuable comments. I would be very grateful. 

List collection is serialized and written into an Xml file
(1) First create a person class, which includes name, age, email three attributes.
<span style="font-family:Microsoft YaHei;font-size:18px;">public class person
{
public string Name
{
set;
get;
}
[XmlIgnore]
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
}</span>Note: [XmlIgnore] in the above code is a non-serializable operation on the Age attribute. This is a non-serializable feature exclusive to Xml serializers. For other objects, use [NonSerialized]. (2) Implement xml serialization by adding data to the list collection
##
<span style="font-family:Microsoft YaHei;font-size:18px;">List<person> list=new List<person> ();
list.Add(new person() { Name = "istari", Age = 22, Email = "1061399756@qq.com" });
list.Add(new person() { Name = "ss", Age = 22, Email = "1061399756@qq.com" });
list.Add(new person() { Name = "ww", Age = 22, Email = "521@qq.com" });
//实现xml序列化
XmlSerializer xml = new XmlSerializer(typeof(List<person>));
using (FileStream fs=File.OpenWrite ("List.xml"))
{
xml.Serialize(fs, list);
}
Console.WriteLine("OK");
Console.ReadKey();</span>(3) Result
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<person>
<Name>istari</Name>
<Email>1061399756@qq.com</Email>
</person>
<person>
<Name>ss</Name>
<Email>1061399756@qq.com</Email>
</person>
<person>
<Name>ww</Name>
<Email>521@qq.com</Email>
</person>
</ArrayOfPerson></span>
The format of the xml file written through this method is the format set internally by the Xml serializer. You don’t need to change it yourself. You can also create a serializer yourself instead of using the system serializer. The next article is for you to share.

The above is the content of XML (5) serialization written to the xml file. For more related content, please pay attention to PHP Chinese Net (m.sbmmt.com)!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20518
7
13631
4
Understanding RSS Documents: A Comprehensive Guide
May 09, 2025 am 12:15 AM
RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.
RSS, XML and the Modern Web: A Content Syndication Deep Dive
May 08, 2025 am 12:14 AM
RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.
How does Python's pickle module handle object serialization, and what are its security implications?
Jun 05, 2025 am 12:12 AM
The Pickle module is not safe because it can execute arbitrary code when deserialized. Python's pickle module is serialized by recording instructions required to rebuild objects, supporting built-in and custom types, but its deserialization process will execute instructions in the byte stream, which may be exploited by malicious data to run shell commands, access files, or create dangerous objects. It is recommended to use pickle only in a trusted environment. When processing untrusted data, you should choose more secure alternatives such as json and yaml, and follow best practices such as loading only trusted data, avoiding public exposure of pickle interfaces, and encrypting signatures for transmitted data.
XML: Is it still used?
May 13, 2025 pm 03:13 PM
XMLisstillusedduetoitsstructurednature,humanreadability,andwidespreadadoptioninenterpriseenvironments.1)Itfacilitatesdataexchangeinsectorslikefinance(SWIFT)andhealthcare(HL7).2)Itshuman-readableformataidsinmanualdatainspectionandediting.3)XMLisusedin
What are the uses of __sleep and __wakeup in PHP?
May 23, 2025 pm 08:00 PM
In PHP, the __sleep and __wakeup methods are called before and after object serialization, respectively, and are used to perform specific tasks. 1) __sleep is used to clean sensitive data, such as clearing passwords in the user management system; 2) __wakeup is used to initialize or reconnect external resources, such as setting a default password. Pay attention to performance overhead, error handling and security when using it.
Mastering Well-Formed XML: Best Practices for Data Exchange
May 14, 2025 am 12:05 AM
Well-formedXMLiscrucialfordataexchangebecauseitensurescorrectparsingandunderstandingacrosssystems.1)Startwithadeclarationlike.2)Ensureeveryopeningtaghasaclosingtagandelementsareproperlynested.3)Useattributescorrectly,enclosingvaluesinquotesandavoidin
Well-Formed XML: The Foundation for Valid XML
Jun 19, 2025 am 12:05 AM
XMLisconsideredwell-formedifitadherestospecificsyntacticrules.Theserulesinclude:1)everyopeningtagmusthaveacorrespondingclosingtag,2)attributesmustbeproperlyquoted,and3)elementsmustbeproperlynested.Ensuringwell-formednessisessentialforcreatingaunivers
XML Data Binding with Castor in Java
Aug 15, 2025 am 03:43 AM
CastorenablesXML-to-Javaobjectmappingviadefaultconventionsorexplicitmappingfiles;1)DefineJavaclasseswithgetters/setters;2)UseUnmarshallertoconvertXMLtoobjects;3)UseMarshallertoserializeobjectsbacktoXML;4)Forcomplexcases,configurefieldmappingsinmappin





