Deserialize From XML - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Deserialize From XML

Demo Code


using System.Xml.Serialization;
using System.IO;// w  ww  .j  a  v  a 2 s  .  co m
using System;

public class Main{
        public static object DeserializeFromXML(System.Type type, string filePath)
      {
         System.IO.FileStream fileStream = null;
         object result;
         try
         {
            fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            result = xmlSerializer.Deserialize(fileStream);
         }
         catch (System.Exception ex)
         {
            throw ex;
         }
         finally
         {
            if (fileStream != null)
            {
               fileStream.Close();
            }
         }
         return result;
      }
}

Related Tutorials