Reader Level:
ARTICLE

Convert your Data Transfer Object to XML (XML Serialization and Deserialization)

Posted by Hasibul Haque Articles | XML February 21, 2011
How to convert Business Entity/ Data Transfer Object to XML string and also XML string to Business Entity/ Data Transfer Object.
  • 0
  • 0
  • 32664
Download Files:
 

Introduction

When we need to transfer our object through the internet or to write to a file then it is necessary to convert it to XML. We can convert the object to XML using XML Serialization. In this article we will discuss how to convert an object (it may be entity / Data Container / DTO) to XML.

Here we will use XML Serialization & Deserialization for converting our object to XML and XML to Object.

XML Serialization:

XML Serialization is the process of saving class member data into an XML. Only public classes and public properties and fields can be serialised â€" methods and private members are not serialized and cannot be deserialized. Private classes cannot be serialized and will result in a compilation error.

Let Crate an Entity Class/ Data Transfer Class

public class UserInfoDTO
    {
 
        public string UserId { get; set; }
        public string UserName { get; set; }
        public string UserAddress { get; set; }
        public
string UserPhone { get; set; }
    }

Now we will fill it with data

UserInfoDTO oUserInfoDTO
                = new UserInfoDTO { UserId = "12", UserName = "Hasibul Haque",
                                    UserAddress = " Dhaka, Bangladesh", UserPhone = "+8801912104674" };

Now we will convert it to XML using XM Serialization.

Before converting to XML we have to add the following namespace. 

using
System.IO;
using System.Xml;
using System.Xml.Serialization;

Let's create a method for converting the object to XML.

public string ToXML(Object oObject)
        {
            XmlDocument
xmlDoc = new XmlDocument();
            XmlSerializer xmlSerializer = new XmlSerializer(oObject.GetType());
            using (MemoryStream xmlStream = new MemoryStream())
            {
                xmlSerializer.Serialize(xmlStream, oObject);
                xmlStream.Position = 0;
                xmlDoc.Load(xmlStream);
                return xmlDoc.InnerXml;
            }
        }

This method will return a string containing XML that you can write to a file or save to a database.

Using ToXML method.

string strXML = ToXML(oUserInfoDTO); 

We will then find the following XML after converting our UserInfoDTO object.

<?xml version="1.0"?>
<UserInfoDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserId>12</UserId>
<UserName>Hasibul Haque</UserName>
<UserAddress> Dhaka, Bangladesh</UserAddress>
<UserPhone>+8801912104674</UserPhone>
</UserInfoDTO>

XML Deserialization:

Deserialization is the reverse process. We will load in an XML then pass the data to the deserializer and it will produce an instance of the class populated with the data. 

public Object XMLToObject(string XMLString, Object oObject)
        {
          
            XmlSerializer
oXmlSerializer = new XmlSerializer(oObject.GetType());
            oObject = oXmlSerializer.Deserialize(new StringReader(XMLString ));
            return oObject;      
        }

Using XMLToObject method:

UserInfoDTO oUserInfoDTO = new UserInfoDTO();
            oUserInfoDTO = (UserInfoDTO)XMLToObject(richTextBox1.Text, oUserInfoDTO);

COMMENT USING