Load serialized object from XML - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Load serialized object from XML

Demo Code


using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;/*from   w w  w .  j a va 2 s.c  om*/

public class Main{
        public static T Load<T>(string path)
        {
            var doc = XDocument.Load(path);
            return Load<T>(doc);
        }
        public static T Load<T>(XDocument doc)
        {
            var serializer = new XmlSerializer(typeof(T));
            var result = (T)serializer.Deserialize(doc.CreateReader());
            return result;
        }
}

Related Tutorials