This method converts an xml string into an object. - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

This method converts an xml string into an object.

Demo Code


using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.IO;/*  www  . java2 s.  c  om*/

public class Main{
        /// <summary>
        /// This method converts an xml string into an object.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static T Deserialise<T>(string xml)
        {
            var xs = new XmlSerializer(typeof (T));
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader(xml, XmlNodeType.Document, null);
                return (T) xs.Deserialize(reader);
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }
}

Related Tutorials