Serializes an XmlDocument to an instance object of type T - CSharp System.Xml

CSharp examples for System.Xml:XML Document

Description

Serializes an XmlDocument to an instance object of type T

Demo Code


using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w w  w . j a  v a  2s.com

public class Main{
        /// <summary>
        /// Serializes an XmlDocument to an instance object of type T
        /// </summary>
        /// <typeparam name="T">The Type of object</typeparam>
        /// <param name="xmlDocument">document to read</param>
        public static T DeserializeFromXmlDocument<T>(XmlDocument xmlDocument) {
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
            XmlNodeReader nodeReader = new XmlNodeReader(xmlDocument.DocumentElement);
            T retVal = default(T);
            try {
                retVal = (T)deserializer.Deserialize(nodeReader);
            }
            finally {
                if (nodeReader != null)
                    nodeReader.Close();
            }
            return retVal;
        }
}

Related Tutorials