Load Object from XML file - CSharp System.Xml

CSharp examples for System.Xml:XML File

Description

Load Object from XML file

Demo Code


using System.Xml.Serialization;
using System.IO;/*from  w w  w.jav  a  2s  .c  o  m*/
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static T Load( string fileName )
        {
            FileStream fs = null;
            T ret = null;
            try
            {
                fs = File.Open( fileName, FileMode.Open, FileAccess.Read, FileShare.Read );
                XmlSerializer xs = new XmlSerializer( typeof( T ) );
                ret = ( T )xs.Deserialize( fs );

                fs.Close();
            }
            catch ( Exception )
            {
                throw;
            }
            finally
            {
                if ( fs != null )
                    fs.Close();
            }

            return ret;
        }
}

Related Tutorials