user146584
user146584

Reputation: 701

.NET XML Serialization Helper Class

I Collected the info from one of the previous StackOverflow Q & A that

The following items can be serialized using the XmlSerializer class:

My Question is how can we develop a XmlSerialize Helper class that takes a Generic Collection as parameter for Xml Serialization.

Upvotes: 2

Views: 4512

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

http://www.codeproject.com/KB/XML/CustomXmlSerializer.aspx?msg=3101055

SUMMARY:CustomXmlSerializer is an alternative to XmlSerializer, supporting both shallow and deep serialization of ArrayLists, Collections, and Dictionaries.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292425

public class XmlSerializationHelper
{
    public static void Serialize<T>(string filename, T obj)
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        using (StreamWriter wr = new StreamWriter(filename))
        {
            xs.Serialize(wr, obj);
        }
    }

    public static T Deserialize<T>(string filename)
    {
        XmlSerializer xs = new XmlSerializer(typeof(T));
        using (StreamReader rd = new StreamReader(filename))
        {
            return (T)xs.Deserialize(rd);
        }
    }
}

(it's not specifically for generic collections, it works for any XML-serializable object)

I'm not sure if that's what you were looking for... if not, please detail what you need

Upvotes: 7

Related Questions