Briefly xml.serialization how to serialize an object to an XML file

Source: Internet
Author: User

Let's briefly describe the structure of the XML file.

In an XML file, "element" is its basic structure, and the entire XML file is made up of several "elements".

Here is an example of an element:

<t name= "Sample" >this is sample</t>

The entire string of characters is called an "element", which consists of several parts. T is called "element" (tag). An element contains the start tag <T> and end tag </T>. The label contains "text", in this case, the this is sample string, or contains child elements (that is, the element's nesting). The Name= "Sample" is called the attribute of element, but "attribute" is not a necessary part of "element", it can be not, there can be several.

An XML file is made up of several elements (element). There can only be one root element (root), and the other elements are child elements of the root element or child elements.

Xml.serialization serializes an object by serializing the object into an XML "element". If a class does not have any attributes, then by default, the serialized "tag" of the class is the class name of the class, and the public fields and public properties of the class are serialized
Chengzi element, the "tag" of the child element is the field name (or attribute name), and the child element text is the field value (or the property value).

With some classes in xml.serialization, you can add some attributes to a class, making it possible to customize it when serializing.

XmlRoot ("Settings"), which indicates that the class is serialized as "root" and "tag" of "root" is the Settings rather than the original class name. Note that there can be only one root element (root) in an XML file.

XmlElement ("T"), which indicates that the class or field (property) is serialized as "element", and that the element's "tag" is T instead of the original class name (field name or property name).

XmlAttribute ("Count"), which indicates that the field (property) is serialized as "attribute", the property name is the field (property) name, and the property value is the field (property) value. This does not seem to apply in array fields (properties).

XmlText, this indicates that the field (property) is serialized as text, and the text value is the field (property) value. However, in a class, only one field (property) is serialized as text, and the serialized element of the class cannot have child elements, meaning that the other fields (properties) of the class can only be serialized as attributes.

XmlIgnore, this indicates that the specified field (property) is not serialized.

In addition to the above, when you encounter a field (property) of a collection type, xml.serialization serializes the field (property) into an element, and each object in that field is serialized as a child element of that element.

You can add the following attributes to a field (property) of a collection type

XmlArray ("E"), this indicates that the field (property) is serialized as "element", "tag" is E.

For example: <xmlarray ("E") >public Student as List (of String) has three values A, B, and C.

The result of serialization is:

<E>

<String>A</String>

<String>B</String>

<String>C</String>

</E>

<xmlarrayitem ("E"), which indicates that each object of the field (property) is serialized as "element" and "tag" is E.

For example: <xmlarrayitem ("E") >public Student as List (of String) has three values A, B, and C.

The result of serialization is:

<Student>

<E>A</E>

<E>B</E>

<E>C</E>

</Student>

You can compare the difference with the above example.

In addition, the above two features can be superimposed.

You can also add XmlElement ("E") to a field (property) of a collection type, which indicates that each object is serialized as element, label (tag) is E, and each object is its child element. In layman's terms, it is parallel serialization.

For example:: <xmlelement ("Lan") >public Student as List (of String) with three values A, B, and C.

The result of serialization is:

<E>

<String>A</String>

</E>

<E>

<String>B</String>

</E>

<E>

<String>C</String>

</E>

The above is a simple description of some classes in the Xml.serialization to control the serialization of objects into XML files. Also hope to communicate with the netizens.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.