CSharp examples for System.Xml:XML String
Method to convert a custom Object to XML string
using System.Xml.Serialization; using System.Xml; using System.Web; using System.Text; using System.Linq; using System.IO;//from w ww . j a v a 2 s. c o m using System.Collections.Generic; using System; public class Main{ ///<summary> /// Method to convert a custom Object to XML string /// </summary> /// <param name="pObject">Object that is to be serialized to XML</param> /// <param name="objectType"></param> /// <returns>XML string</returns> public static String SerializeObject(Object pobject, Type objectType) { try { String XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Indent = true; XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add(string.Empty, string.Empty); //XmlSerializer xs = new XmlSerializer(objectType); XmlSerializer xs = new XmlSerializerCache()[objectType]; XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, pobject, namespaces); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString; } catch (Exception e) { throw e; } } }