XML(5) serialization to write xml file

黄舟
Release: 2017-02-10 16:33:58
Original
1547 people have browsed it

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)!

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template