How to serialize List<object>

Posted by afin on Stack Overflow See other posts from Stack Overflow or by afin
Published on 2010-04-20T23:57:01Z Indexed on 2010/04/21 0:03 UTC
Read the original article Hit count: 534

Filed under:
|

I am writing common functions to serialize the given object and List<object> as follows

public string SerializeObject(Object pObject)// for given object
        {
            try
            {
                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(pObject));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xs.Serialize(xmlTextWriter, pObject);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;
            }
            catch (Exception e) { System.Console.WriteLine(e); return null; }
        }

public string SerializeObject(List<Object> pObject)// for given List<object>
    {
        try
        {
            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(typeof(pObject));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, pObject);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
        }
        catch (Exception e) { System.Console.WriteLine(e); return null; }
    }

first one is working fine. If I pass any type, it is successfully returning xml string. But second one is throwing error. what could be wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about xml-serialization