This example is based on converting class objects to XML and XML
to class objects. In many cases we need to achieve this.
Example
Communicating with Web service etc.
Step 1:- SerializeData Method
public static string SerializeData(object o)
{
XmlSerializer xmlSerializer
= new XmlSerializer(o.GetType());
StringWriter stringWriter
= new StringWriter();
XmlTextWriter xmlTextWriter
= new XmlTextWriter(stringWriter);
try
{
xmlSerializer.Serialize(xmlTextWriter, o);
return stringWriter.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
stringWriter.Close();
xmlTextWriter.Close();
}
}
Step 2:- DeserializeData Method
public static object DeserializeData(string strXML, object o)
{
XmlSerializer xmlSerializer
= new XmlSerializer(o.GetType());
StringReader stringReader
= new StringReader(strXML);
XmlTextReader xmlTextReader
= new XmlTextReader(stringReader);
try
{
object obj
= xmlSerializer.Deserialize(xmlTextReader);
return obj;
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
stringReader.Close();
xmlTextReader.Close();
}
}
Step 3:- Complete Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace XmlSerializeDeserialize
{
public class MailMessage
{
public string From
{ get; set; }
public string To
{ get; set; }
public string Subject
{ get; set; }
public string Body
{ get; set; }
}
class Program
{
public static string SerializeData(object o)
{
XmlSerializer xmlSerializer
= new XmlSerializer(o.GetType());
StringWriter stringWriter
= new StringWriter();
XmlTextWriter xmlTextWriter
= new XmlTextWriter(stringWriter);
try
{
xmlSerializer.Serialize(xmlTextWriter, o);
return stringWriter.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
stringWriter.Close();
xmlTextWriter.Close();
}
}
public static object DeserializeData(string strXML, object o)
{
XmlSerializer xmlSerializer
= new XmlSerializer(o.GetType());
StringReader stringReader
= new StringReader(strXML);
XmlTextReader xmlTextReader
= new XmlTextReader(stringReader);
try
{
object obj
= xmlSerializer.Deserialize(xmlTextReader);
return obj;
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
stringReader.Close();
xmlTextReader.Close();
}
}
static void Main(string[]
args)
{
MailMessage mailMessage
= null;
//
Initializer member declarator
mailMessage = new MailMessage()
{
From = "Ram",
To = "Shyam",
Subject = "Reminder",
Body = "Don't forget me this weekend!"
};
string serializedMailMessage
= Program.SerializeData(mailMessage);
Console.WriteLine("__________________Serialized
MailMessage Data____________________");
Console.WriteLine(serializedMailMessage);
//
Create a new object of MailMessage
mailMessage = new MailMessage();
Console.WriteLine("__________________Deserialized
MailMessage Data__________________");
mailMessage = (MailMessage)Program.DeserializeData(serializedMailMessage,
mailMessage);
Console.WriteLine("From
- " + mailMessage.From);
Console.WriteLine("To
- " + mailMessage.To);
Console.WriteLine("Subject
- " + mailMessage.Subject);
Console.WriteLine("Body
- " + mailMessage.Body);
Console.ReadKey();
}
}
}
Step
4:- Output
No comments:
Post a Comment