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;

Dinesh BeniwaleditedPosted Feb 21, 2011, 3:48 AMEdited Feb 21, 2011, 3:50 AM
Welcome to the C# Corner Community.