Deserialize From XML File - CSharp System.Xml

CSharp examples for System.Xml:XML Serialization

Description

Deserialize From XML File

Demo Code

//    This program is free software: you can redistribute it and/or modify
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.IO;/*w w w . ja va  2  s . c  o m*/
using System;

public class Main{
        public static object DeserializeFromFile(string aFileName, Type aType)
        {
            XmlSerializer lSerializer = new XmlSerializer(aType);
            FileStream lStream = new FileStream(aFileName, FileMode.Open);
            XmlTextWriter lWriter = new XmlTextWriter(lStream, Encoding.UTF8);

            return lSerializer.Deserialize(lStream);
        }
        /// <summary>
        /// XML string to object
        /// </summary>
        /// <param name="aXMLString">A valid XML string<</param>/
        /// <param name="aType">Type of result</param>
        /// <returns>object</returns>
        public static object Deserialize(string aXMLString, Type aType)
        {
            XmlSerializer lSerializer = new XmlSerializer(aType);
            MemoryStream lMemStream = new MemoryStream((new UTF8Encoding()).GetBytes(aXMLString));
            XmlTextWriter lWriter = new XmlTextWriter(lMemStream, Encoding.UTF8);

            return lSerializer.Deserialize(lMemStream);
        }
}

Related Tutorials