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

Serialize and DeSerialize generic Array

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
16 Jan 2012CPOL 29.1K   3   4
Serialize and DeSerialize generic Array
We often use things like Configuration[] configs and like to manage that in an XML file you can edit. Two simple extension methods can help you.

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 = new System.Xml.Serialization.XmlSerializer(typeof(T[]), root);


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

Thanks for the following articles:
XmlSerializer Constructor (Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String)[^]

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

 
GeneralLooks like a very useful tip/trick, thanks. Just curious abo... Pin
BillWoodruff16-Jan-12 22:48
professionalBillWoodruff16-Jan-12 22:48 
GeneralVisit this project for (de)serializing complex objects http:... Pin
vfvbaten2-Jan-12 21:50
vfvbaten2-Jan-12 21:50 
QuestionOr use a generator Pin
vfvbaten2-Jan-12 21:52
vfvbaten2-Jan-12 21:52 
General:) Pin
raananv2-Jan-12 9:02
raananv2-Jan-12 9:02 

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.