C Object Serialization Xml

2 Jan 2015CPOL
  • General familiarity with XML; General familiarity with Visual C#; back to the top XML Serialization. Serialization is the process of taking the state of an object and persisting it in some fashion. The Microsoft.NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this.
  • The.NET framework contains many classes to help with this process, and offers in-built support for XML serialization (serializing an object to an XML data file) through the XmlSerializer class and the System.Xml.Serialization library. This article provides a brief overview of XML serialization and deserialization in the C# programming language.
  • Mar 02, 2008  Unmanaged C doesn't have anything resembling reflection; RTTI can give you the type of a known symbol, but there's no way to enumerate the symbols. So, in the end, there's no way around having to write a serialize method for each class. As to which serialization framework to use, I highly recommend Boost's serialization library.
  • XML serialization serializes an object into an XML document. System.Xml.Serialization namespace provides methods for converting objects into XML and write into XML files. XML serialization provides greater interoperability and better forward-compatibility.

Introduction

XML serialization. XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to XML. First, we create an object of the Tutorial class. We then assign the value of '1' to ID and '.net' to the name property. We then use the formatter class which is used to serialize or convert the object to a binary format. The data in the file in serialization is done in binary format. Next, we create a file stream object.

The article talks about serialization of objects in XML format and deserialization of an XML file back to an object. Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type. Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport.

Let's start with a basic example. Here is a simple class that needs to be serialized:

The following points should be noted while creating a class for serialization:

  1. XML serialization only serializes public fields and properties.
  2. XML serialization does not include any type information.
  3. We need to have a default/non-parameterised constructor in order to serialize an object.
  4. ReadOnly properties are not serialized.

Code to serialize the above class:

XmlSerializer (located in the System.Xml.Serialization namespace) class is used to serialize and deserialize. The class method Serialize is called. Since we have to serialize in a file, we create a 'TextWriter'. Since TextWriter implements IDisposable, we used using so that we need not close the writer.

The output after the serialization is:

Here, in the XML, we can see that the Head tag of the XML created is the same as that of the class name and the subtag names are the same as the properties in class AddressDetails. Each public property is displayed in the form of Tags in the XML created. We can observe here that only public fields are displayed here.

XML Serialization and Attributes

Some common attributes that are available during Serialization are:

  • XmlAttribute: This member will be serialized as an XML attribute.
  • XmlElement: The field will be serialized as an XML element.
  • XmlIgnore: Field will be ignored during Serialization.
  • XmlRoot: Represent XML document's root Element.

Use of XmlElement

Further, if we need to have different Tag name in XML from the class property Name. We can introduce the XmlElement attribute to it in the class structure.

[XmlElement('Number')] specifies that the property HouseNo will be serialized with the Tag name 'Number' in the XML File. It helps us to map between the XML Tag name and the class Property Name. The resultant XML string with the Custom tag name is given below:

Use of XmlAttribute

If we want that the property HouseNo should occur as the attribute for the Tag AddressDetails, then we should use XmlAttribute. XmlAttribute serializes the object property as the attribute for the parent tag. The following code illustrates the functionality:

The XML serialized output for the code will be:

Notice here, since the class property HouseNo is specified as XMLAttribute, therefore this property is an Attribute for the parent tag AddressDetails.

Use to XmlIgnore

By default, all public fields and public read/write properties are serialized by the XmlSerializer. That is, the value of each public field or property is persisted as an XML element or XML attribute in an XML-document instance. In order to override this property, apply XmlIgnore attribute to it. This will remove the element from the XML. The code below explains the following:

Here, we can see that the property City contains XmlIgnore attribute. The resultant XML created won't contain the City tag in it.

Notice here that the property City is not serialized because of the attribute XmlIgnore placed on it.

Use of XmlRoot

Every XML has a root element. By default, the name of the root element is the same as the name of the class that is serialized. In order to give a custom name to the root element of XML, we use XmlRoot attribute. Implementation of this attribute is provided below:

Here, we can see that the attribute XmlRoot is placed over AddressDetails class. This will now override the default serialization behavior which takes XML tag root name same as the class name. The XML will now have 'Root' as the root tag.

C Object Serialization Xml Files

Notice here that the root tag here is now 'Root' and not the Class name.

Object List Serialization

Now let's try to serialize a list of AddressDetails object to XML file:

The XML output for the above execution will be: Shadowhunters full episodes.

Notice that the XML produced gives a list of AddressDetails object.

Serialization of Classes Containing Other Class Objects

If we have a class structure such that a class contains an object of other class and we want to include that class object also for serialization. Let's take a look at the following example:

This is how the PersonalDetails class will be serialized:

To add more complexity to it, let's try creating the following XML structure:

Observe that the difference over here is that we need to have 'HouseNo' as the attribute for the Address Tag. Let's see what change will be made in class in order to create this structure:

As per the requirement, we wanted to have 'HouseNo' as XML attribute instead of the normal XMLElement. Therefore, we introduce 'XmlAttribute' on property.

Let's try creating the following XML structure:

The difference over here is that we require the StreetName as innertext of the XML node 'address'. So in order to create such structure, we have another attribute XmlText. This helps us to add the particular property as innertext for a tag.

C Object Serialization Xml Tutorial

So the code for creating such a structure is:

The 'XmlText' attribute here adds StreetName as InnerText to the tag 'address'

Deserialization of XML

See the following article for details on deserialization: XML Serialization and Deserialization (Part 2).

Conclusion

C# Custom Xml Serialization

Serialization is a very efficient way to convert the object to XML. This save lots of time and effort.