Wednesday, September 4, 2013

Serialize the class object into XML

Sometimes there is requirement to convert/serialize an object into the XML format so that we can do some operation and manipulation based on the XML data. Here is the simple trick to convert the object data into XML format.

This piece of code will give you the result of Serialized XML by converting an object.

void SerializeXml<T>(string filePath, object objectToSerialize)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                xmlSerializer.Serialize(fs, objectToSerialize);
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
            catch (Exception ex)
            {              
            }
        }

No comments:

Post a Comment

Put your comments here

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।