Serialize the object to XML stream - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Serialize the object to XML stream

Demo Code

//  Licensed under the GPLv2: http://dotnetage.codeplex.com/license
using System.Xml.Serialization;
using System.Xml;
using System.IO;//from   w w  w . ja v  a 2 s . com
using System;

public class Main{
        public static void SerizlizeToStream<T>(Stream stream,T instance)
        {
            SerizlizeToStream(stream,typeof(T),instance);
        }
        /// <summary>
        /// Serialize the object to stream
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="type"></param>
        /// <param name="instance"></param>
        public static void SerizlizeToStream(Stream stream, Type type, object instance)
        {
            stream.Position = 0;
            XmlSerializer ser = new XmlSerializer(type);
            ser.Serialize(stream, instance);
        }
}

Related Tutorials