Serialization is the process of converting an object into an easily transportable data stream in memory or on disk. For example, you can serialize an object and transport it over the Internet via HTTP between a client and a server. At the other end, deserialization reconstructs the object from the data stream.
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The World Wide Web Consortium's XML Specification and several other related specifications define XML. The design goals of XML emphasize simplicity, generality, and usability across the Internet. It is a textual data format with strong support via Unicode for different human languages. Although the design of XML focuses on documents, the language is widely used for the representation of arbitrary data structures such as those used in web services.
In this guide we will learn how to serialize an object to the XML format :
XML serialization serializes only the public fields and property values of an object in an XML data stream. XML serialization does not include type information.
xsd2code++ proposes to automatically generate the attributes to serialize your objects so that they correspond 100% to the original schema.
To enable XML serialization, please refer to the common elements for serialization. This page details the options that are specific to XML serialization.
For illustration purposes, we will use the XSD schema example which can be found in the introduction page of the documentation.
To serialize with XML, you just have to choose it in the xsd2code options:
Here's a section of the code that should be generated
Once you've ran the xsd2code generation to completion you will have the c# class that will correspond to the shown schema, depending on the chosen settings you might have less or more than you expected, we will now see some of these settings
This parameter defines whether the XML file will be on a single line or formatted.
<?xml version="1.0" encoding="UTF-8"?><PurchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><BillTo><name>James</name><street>19 av road street</street><country>us</country></BillTo></PurchaseOrder>
<?xml version="1.0" encoding="utf-8"?> <PurchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <BillTo> <name>James</name> <street>19 av road street</street> <country>us</country> </BillTo> </PurchaseOrder>
Indicates the indentation level for XML formatting.
Indicates that each attribute is positioned on a new line.
true to omit the XML declaration; otherwise, false. The default is false, an XML declaration is written.
xmlns means "XML name space". This option allows to define it in the XML header.