WCF Serialize and DeSerialize XML Sample

This is a little example how to Serialize Object to XML and Deserialize XML to Object using a simple generic function with c# on WCF Application.

This code take it and change it to one of the references listed at the end and you can download the project from here!.

So first we can create our Class

[DataContract]
public class Person : IExtensibleDataObject
{
    [DataMember()]
    public string FirstName;
    [DataMember]
    public string LastName;
    [DataMember()]
    public int ID;
	
    public Person(string newfName, string newLName, int newID)
    {
        FirstName = newfName;
        LastName = newLName;
        ID = newID;
    }
    public Person() { }
    private ExtensionDataObject extensionData_Value;
    // this illustrates using the versioning ExtensionData property, which is not used
    public ExtensionDataObject ExtensionData
    {
        get { return extensionData_Value; }
        set { extensionData_Value = value; }
    }
}

then our Generic Serialize and Deserialize functions

 public class GenericDataContractSerializer<T>
{
    public static string SerializeObject(T obj)
    {
        try
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            var stringBuilder = new StringBuilder();
            var stringWriter = new StringWriter(stringBuilder);
            xmlSerializer.Serialize(stringWriter, obj);
			
            return stringBuilder.ToString();
        }
        catch (Exception exception)
        {
            throw new Exception("Failed to serialize data contract object to xml string:", exception);
        }
    }
    /// <summary>
    /// DeserializeXml
    /// </summary>
    /// <param name="xml"></param>
    /// <returns></returns>
    public static T DeserializeXml(string xml)
    {
        try
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            return (T)xmlSerializer.Deserialize(new StringReader(xml));
        }
        catch (Exception exception)
        {
            throw new Exception("Failed to deserialize xml string to data contract object:", exception);
        }
    }
}

Our interface Service

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string Serialize();

    [OperationContract]
    Person DesSerialize(string xmlString);
}

Our functions inheritance from Interface

 public class Service1 : IService1
{
    public string Serialize()
    {
        Person person = new Person("Santa", "Clause", 0929);
        string xmlString = GenericDataContractSerializer<Person>.SerializeObject(person);
        return xmlString;
    }

    public Person DesSerialize(string xmlString)
    {
        Person dPerson = GenericDataContractSerializer<Person>.DeserializeXml(xmlString);
        return dPerson;
    }
}

So if you are working with EF you can use things like Automapper to mapping the new object to the Entity object and Insert or Update the data.

and here is some great references: