views:

299

answers:

2

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

The following items can be serialized using the XmLSerializer class:


  • •Public read/write properties and fields of public classes
  • •Classes that implement ICollection or IEnumerable
  • •XmlElement objects
  • •XmlNode objects
  • •DataSet objects

  • My Question is how can we develop a XMlSerilize Helper class that takes Generic Collection as parameter for Xml Serilization.

    +2  A: 
    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

    Thomas Levesque
    That is what exactly I am looking for ! Thank you very much.
    That won't work for dictionaries.
    Will
    @Will : that's why I said "any **XML-serializable** object"... A dictionary is not XML serializable (unless it implements IXmlSerializable, see this link : http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx)
    Thomas Levesque
    A: 

    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.

    adatapost
    oh ! Thanks for sharing the info.