How to Convert C# Class Object to XML and Vice Versa

Description:

   XML is a well structured text file, we can store any kind structured data in XML file. In most of the times our applications demand us to maintain a structured data in XML or Database. C# class object is one of the well structured data. In dynamic applications, we can store and retrieve C# class object from XMl file as per our need. In this article I am going write C# code Convert C# Class Object to XML and Vice Versa.

Summary:

Convert C# Class Object to XML string using XmlSerializer

You can easily convert a C# Class Object into XML string using XmlSerializer and MemoryStream

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

namespace MorganUtil
{
    public class UserDetail
    {
        public string UserName { get; set; }
        public string MailID { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            UserDetail userDetail = new UserDetail { UserName = "Morgan", 
                MailID = "[email protected]" };

            string xmlString = ConvertObjectToXMLString(userDetail);
            // Save C# class object into Xml file
            XElement xElement = XElement.Parse(xmlString);
            xElement.Save(@"C:\UsersAdministratorDesktopuserDetail.xml");
        }

        static string ConvertObjectToXMLString(object classObject)
        {
            string xmlString = null;
            XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType());
            using (MemoryStream memoryStream = new MemoryStream())
            {
                xmlSerializer.Serialize(memoryStream, classObject);
                memoryStream.Position = 0;
                xmlString = new StreamReader(memoryStream).ReadToEnd();
            }
            return xmlString;
        }
    }
}

Xml Output for single class object:

How to Convert C# Class Object to XML and Vice Versa

Xml Output for list of class object(List<UserDetail>): You will get below Xml data when you convert list of class object (List<UserDetail>) into Xml.

Convert C# Class Object to XML and XML String into C# Class Object

Convert XML string to C# Class Object using XmlSerializer

Use the below function to convert XML string into C# Class Object using XmlSerializer and StringReader.

static void Main(string[] args)
{
    string xmlString = @"<UserDetail>
                            <UserName>Morgan</UserName>
                            <MailID>[email protected]</MailID>
                            </UserDetail>";
    //Note: you can also read xmlString from Xml file by using below code
    // XElement xmlObject = XElement.Load(@"C:\UsersAdministratorDesktopuserDetail.xml");
    //string xmlString = xmlObject.ToString();

    UserDetail userDetail = (UserDetail)ConvertXmlStringtoObject<UserDetail>(xmlString);
}

static T ConvertXmlStringtoObject<T>(string xmlString)
{
    T classObject;

    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    using (StringReader stringReader = new StringReader(xmlString))
    {
        classObject = (T)xmlSerializer.Deserialize(stringReader);
    }
    return classObject;
}

Thanks,
Morgan
Software Developer


Advertisement

Leave a Comment