Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Tip/Trick

Serialize and deserialize generic arrays

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 Dec 2011CPOL 32.6K   8   4
Code to serialize and deserialize generic arrays.

We often use things like Configuration[] configs and like to manage that in an XML file that we can edit. Two simple extension methods can help you here.


C#
public static string SerializeArray(this T[] list)
{
    System.Xml.XmlDocument doc = new XmlDocument();
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(list.GetType());
    System.IO.MemoryStream stream = new System.IO.MemoryStream();

    try
    {
        serializer.Serialize(stream, list);
        stream.Position = 0;
        doc.Load(stream);
        return doc.InnerXml;
    }
    catch { throw; }
    finally
    {
        stream.Close();
        stream.Dispose();
    }
}

public static T[] DeSerializeArray(string serializedData)
{
    T[] list = null;
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T[]));
    XmlReader xReader = XmlReader.Create(new StringReader(serializedData));

    try
    {
        list = (T[])serializer.Deserialize(xReader);
    }
    catch
    {
        throw;
    }
    finally
    {
        xReader.Close();
    }

    return list;
}

Code to serialize the data:


C#
string xml1 = _environments.SerializeArray();
StreamWriter writer = new StreamWriter("config.xml");
writer.Write(xml1);
writer.Close();

To deserialize the data:


C#
string xml = File.ReadAllText("config.xml");
_environments = Extensions.DeSerializeArray(xml);

If you don't like the root ArrayOfConfiguration, you can change the extension methods to use a XmlRootAttribute like:


C#
XmlRootAttribute root = new XmlRootAttribute(rootName);
System.Xml.Serialization.XmlSerializer serializer = 
   ew System.Xml.Serialization.XmlSerializer(typeof(T[]), root);

You need to use the matching name in both methods to get it working.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 These kind of examples really helps ... Pin
RunningRob2-Jan-12 19:58
RunningRob2-Jan-12 19:58 
GeneralOk I sort of get it. Do you have to have a XML schema set u... Pin
verber0012-Jan-12 15:59
verber0012-Jan-12 15:59 
GeneralRe: the problem I encountered was that some one created a List o... Pin
H. Tony3-Jan-12 7:07
H. Tony3-Jan-12 7:07 
GeneralForgive the noob question but what exactly does this code do... Pin
verber0012-Jan-12 15:48
verber0012-Jan-12 15:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.