Serialize and deserialize objects as Xml using generic types in C# 2.0

I've been using Object<>Xml serialization in .NET based on this post for a while. I decided to enhance it a tad bit with some generics, so I could use it more broadly. Here's what I came up with:

using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

/// <summary>
///
To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
///
</summary>
///
<param name="characters">Unicode Byte Array to be converted to String
</param>
///
<returns>String converted from Unicode Byte Array
</returns>
private static string UTF8ByteArrayToString(byte[] characters)
{
   UTF8Encoding encoding = new UTF8Encoding();
   string constructedString = encoding.GetString(characters);
   return (constructedString);
}

/// <summary>
///
Converts the String to UTF8 Byte array and is used in De serialization
///
</summary>
///
<param name="pXmlString"></param>
///
<returns></returns>
private static Byte[] StringToUTF8ByteArray(string pXmlString)
{
   UTF8Encoding encoding = new UTF8Encoding();
   byte[] byteArray = encoding.GetBytes(pXmlString);
   return byteArray;
}

/// <summary>
///
Serialize an object into an XML string
///
</summary>
///
<typeparam name="T"></typeparam>
///
<param name="obj"></param>
///
<returns></returns>
public static string SerializeObject<T>(T obj)
{
   try
   {
      string xmlString = null;
      MemoryStream memoryStream = new MemoryStream();
      XmlSerializer xs = new XmlSerializer(typeof(T));
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
      xs.Serialize(xmlTextWriter, obj);
      memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
      xmlString = UTF8ByteArrayToString(memoryStream.ToArray());      return xmlString;
   }
   catch
   {
      return string.Empty;
   }
}

/// <summary>
///
Reconstruct an object from an XML string
///
</summary>
///
<param name="xml"></param>
///
<returns></returns>
public static T DeserializeObject<T>(string xml)
{
   XmlSerializer xs = new XmlSerializer(typeof(T));
   MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
   XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
   return (T)xs.Deserialize(memoryStream);
}

Print | posted @ Friday, July 20, 2007 8:35 AM

Comments on this entry:

Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by Manohar at 7/24/2007 7:47 AM

Great...thanks for help.
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by Will Asrari at 8/3/2007 12:42 PM

It seems that I wasn't the only one that edited that original codebase for C# 2.0! The only main difference between our code samples is that I implemented using constructs.

http://www.willasrari.com/blog/xml-serialization-with-c/000231.aspx
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by TorstenR at 2/7/2008 10:06 PM

You should sourround the usage of the memorystream with a "using()".
Another suggestion: use a StreamReader instead of the encoding like so:

memoryStream.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(memoryStream)) {
return sr.ReadToEnd();
}
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by kesulin at 2/19/2008 9:52 PM

very useful information!

thanks very much!
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by Too bad at 3/30/2008 3:58 PM

Very bad informations!
You have to try hard!
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by Messaoud OUBECHOU at 5/31/2008 5:31 AM

Great !
Thanx a lot,
messaoud
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by Rafael at 8/4/2008 10:36 AM

It's possible serialize a array, for example object[ ] ??
Gravatar #  Serialize and deserialize objects as Xml using generic types in C# 2.0
by syam at 10/4/2008 4:09 AM

what is deserialization and how to see the output for deserialization afetr it is serialized.
plz provide me an example for deserialization followed by serialization.
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by JAcooL at 10/28/2008 2:39 AM

private static string FormatingXml(string sUnformattedXml)
{
XmlDocument xd = new XmlDocument();
xd.LoadXml(sUnformattedXml);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xtw = null;
try
{
xtw = new XmlTextWriter(sw);
xtw.Formatting = Formatting.Indented;
xd.WriteTo(xtw);
}
finally
{
if (xtw != null)
xtw.Close();
}
return sb.ToString();
}
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by lokritan at 11/1/2008 8:29 AM

It's possible serialize a array,
Gravatar # re: Serialize and deserialize objects as Xml using generic types in C# 2.0
by anandarajeshwaran at 12/22/2008 2:13 AM

using your code i have converted my object to xml string

"return xmlString;"

Instead of returning an xml string i want to store this data as a xml file on the disk.

How can i do that do reply

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 8 and 5 and type the answer here: