Save Object as XML - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Save Object as XML

Demo Code


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

public class Main{
        public bool Save( string fileName )
        {
            T data = this as T;

            if ( data != null )
                return Save( fileName, data );
            else
                return false;
        }
        public static bool Save( string fileName, T data )
        {
            FileStream fs = null;
            try
            {
                fs = File.Create( fileName );
                XmlSerializer xs = new XmlSerializer( typeof( T ) );
                xs.Serialize( fs, data );
                fs.Close();

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

Related Tutorials