Serialize To Xml - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Serialize To Xml

Demo Code


using System.Xml.Serialization;
using System.IO;// w  w w.j ava2  s.  co  m
using System;

public class Main{
        public static bool SerializeToXml(object obj, string filePath)
      {
         bool result = false;
         System.IO.FileStream fileStream = null;
         try
         {
            fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
            XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
            xmlSerializer.Serialize(fileStream, obj);
            result = true;
         }
         catch (System.Exception ex)
         {
            throw ex;
         }
         finally
         {
            if (fileStream != null)
            {
               fileStream.Close();
            }
         }
         return result;
      }
}

Related Tutorials