Method to reconstruct an Object from XML string - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

Method to reconstruct an Object from XML string

Demo Code


using System.Xml.Serialization;
using System.Xml;
using System.Web;
using System.Text;
using System.Linq;
using System.IO;// ww w . jav a  2 s.c  o  m
using System.Collections.Generic;
using System;

public class Main{
        /// <summary>
        /// Method to reconstruct an Object from XML string
        /// </summary>
        /// <param name="pXmlizedString"></param>
        /// <param name="objectType"></param>
        /// <returns></returns>
        public static Object DeserializeObject(String pstring, Type objectType)
        {
            if (string.IsNullOrEmpty(pstring))
                return null;
            //XmlSerializer xs = new XmlSerializer(objectType);
            XmlSerializer xs = new XmlSerializerCache()[objectType];

            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pstring));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            return xs.Deserialize(memoryStream);
        }
}

Related Tutorials