CSharp examples for System.Xml:XML Serialization
Serialize Object to XML
using System.Xml.Serialization; using System.Text; using System.IO;/*from www . j a v a 2 s. co m*/ using System; public class Main{ public static string Serialize<T>(T t) { MemoryStream stream = new MemoryStream(); StreamWriter textWriter = new StreamWriter(stream, Encoding.GetEncoding("GBK")); XmlSerializer serializer = new XmlSerializer(typeof(T)); XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add(string.Empty, string.Empty); serializer.Serialize(textWriter, t, namespaces); string str = Encoding.GetEncoding("GBK").GetString(stream.ToArray()).Replace("\r", "").Replace("\n", ""); while (str.Contains(" <")) { str = str.Replace(" <", "<"); } return str.Substring(str.IndexOf("?>") + 2); } }