Home / Blog / Documentation / Code Generator / Settings / Serialization / XML Serialization
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>
This settings indicates the indentation level for XML formatting.
This setting indicates wether or not each attribute is positioned on a new line.
This setting indicates the status of the XML declaration; true to omit the XML declaration; otherwise, false. The default is false, an XML declaration is written.
The xsi:schemaLocation attribute locates schemas for elements and attributes that are in a specified namespace. Its value is
is a sequence of alternating namespace URI's followed by relative or absolute URL's where the schema for that namespace can be found. You can find a step by step guide detailing the use of this settings in our
xmlns means "XML name space". This option allows to define it in the XML header.