SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Hasibul Haque Articles | XML in C# February 21, 2011
How to convert Business Entity/ Data Transfer Object to XML string and also XML string to Business Entity/ Data Transfer Object.
Reader Level:
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);

Login to add your contents and source code to this article
share this article :
post comment
 

Welcome to the C# Corner Community.

Posted by Dinesh Beniwal Feb 21, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor