c# object to xml

C# Object To XML and Vice Versa using C#

Sometimes in ASP.NET web applications we like to save or send our data as XML to the SQL databases. It’s mostly done when there is a large chunk of data or we like to convert our entity model objects to XML. It makes it easier to move data from web applications to SQL database and vice versa. Here I’ll share a code snippet to convert C# object to XML and XML to object C#.

Also read : Why do you Need An Interface in C# ?

C# Object to XML

To convert an object to XML, we’ll make use of XmlSerializer to serialize and XmlTextWriter to output the XML string. Here is how the code looks like :

public static string GetXMLFromObject(object o)
{
    StringWriter sw = new StringWriter();
    XmlTextWriter tw = null;
    try
    {
        XmlSerializer serializer = new XmlSerializer(o.GetType());
        tw = new XmlTextWriter(sw);
        serializer.Serialize(tw, o);
    }
    catch (Exception ex)
    {
        //Handle Exception Code
    }
    finally
    {
        sw.Close();
        if (tw != null)
        {
            tw.Close();
        }
    }
    return sw.ToString();
}

XML to Object C#

Similarly, to convert an XML string to object we make use of the XmlSerializer to deserialize and XmlTextReader to read the XML string. Here is how the code to convert XML to objects looks like :

public static Object ObjectToXML(string xml, Type objectType)
{
    StringReader strReader = null;
    XmlSerializer serializer = null;
    XmlTextReader xmlReader = null;
    Object obj = null;
    try
    {
        strReader = new StringReader(xml);
        serializer = new XmlSerializer(objectType);
        xmlReader = new XmlTextReader(strReader);
        obj = serializer.Deserialize(xmlReader);
    }
    catch (Exception exp)
    {
        //Handle Exception Code
    }
    finally
    {
        if (xmlReader != null)
        {
            xmlReader.Close();
        }
        if (strReader != null)
        {
            strReader.Close();
        }
    }
    return obj;
}

Let’s try to use the above code snippets to convert an object to XML and vice versa. Let’s create a class called Employee. Here is how it looks like :

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Now let’s create an instance of the Employee class, fill it up and convert it into XML.

Employee emp = new Employee();
emp.FirstName = "Code";
emp.LastName = "Handbook";

string xml = GetXMLFromObject(emp);

Try running the above code and you should be able to get the XML string corresponding to the Employee instance. Here is the XML string :

  <?xml version="1.0" encoding="utf-16" ?> 
- <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName>Code</FirstName> 
  <LastName>Handbook</LastName> 
  </Employee>

Now to convert the above generated XML string back to Employee object use the following code:


Object obj = ObjectToXML(xml,typeof(Employee));

And you should have the XML converted into Employee object in the obj variable.

Wrapping It Up

In this short tutorial, we saw how to convert C# object to XML and XML to object C# using ASP.NET. Also have a look at how to encode and decode JSON data in ASP.NET web application. Do let us know your thoughts, suggestions, or any issues in the comments below.