using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Xml; using System.Xml.Serialization; namespace Common { public class XmlConverter { public static string Serialize<T>(T obj) { //XmlSerializer serializer = new XmlSerializer(typeof(T)); //StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); //serializer.Serialize(writer, obj); //string xml = writer.ToString(); //writer.Close(); //writer.Dispose(); //return xml; string OutputXmlString = string.Empty; using (MemoryStream ms = new MemoryStream()) { var setting = new XmlWriterSettings() { Encoding = new UTF8Encoding(false), Indent = true, }; using (XmlWriter writer = XmlWriter.Create(ms, setting)) { XmlSerializer xmlSearializer = new XmlSerializer(typeof(T)); xmlSearializer.Serialize(writer, obj); OutputXmlString = Encoding.UTF8.GetString(ms.ToArray()); } } return OutputXmlString; } public static T Deserialize<T>(string xml) { //XmlSerializer serializer = new XmlSerializer(typeof(T)); //StringReader reader = new StringReader(xml); //T result = (T)(serializer.Deserialize(reader)); //reader.Close(); //reader.Dispose(); //return result; using (MemoryStream MS = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { using (XmlReader xr = XmlReader.Create(MS)) { XmlSerializer xmlSearializer = new XmlSerializer(typeof(T)); return (T)xmlSearializer.Deserialize(xr); } } } } }