Deserialize the object from XML stream - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Deserialize the object from 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 .j  a  va2  s.com*/
using System;

public class Main{
        public static T DeserializeFormStream<T>(Stream stream)
        {
            return (T)DeserializeFormStream(stream, typeof(T));
        }
        /// <summary>
        /// Deserialize the object from stream
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object DeserializeFormStream(Stream stream, Type type)
        {
            stream.Position = 0;
            XmlSerializer ser = new XmlSerializer(type);
            object obj = ser.Deserialize(stream);
            return obj;
        }
}

Related Tutorials