Table of Contents
What is serialization first? What does it do? " >What is serialization first? What does it do?
Serialization" >Serialization
Home Backend Development XML/RSS Tutorial XML(5) serialization to write xml file

XML(5) serialization to write xml file

Feb 10, 2017 pm 04:33 PM
xml Serialization

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. XML(5) serialization to write xml fileXML(5) serialization to write xml file

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>
Copy after login

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>
Copy after login

(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>
Copy after login


PS

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.

XML(5) serialization to write xml file


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 (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

How Python parses XML files How Python parses XML files Aug 09, 2023 am 11:48 AM

How Python parses XML files XML (eXtensibleMarkupLanguage) is a markup language used to represent structured data. When processing XML data, we often need to parse the XML file to extract the required information. Python provides many libraries and modules to parse XML files, such as ElementTree, lxml, etc. This article will introduce how to use Python to parse XML files, with code examples. In Python,

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

In Java, how can we serialize a list of objects using flexjson? In Java, how can we serialize a list of objects using flexjson? Sep 05, 2023 pm 11:09 PM

Flexjson is a lightweight library for serializing and deserializing Java objects to and from JSON format. We can serialize a list of objects using the serialize() method of the JSONSerializer class. This method performs shallow serialization on the target instance. We need to pass a list of objects of list type as a parameter to the serialize() method. Syntax publicStringserialize(Objecttarget) example importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerial

See all articles