/// <summary> /// Retuns XML String representation of surce with UTF-8 encoding. /// </summary> /// <param name="source"></param> /// <returns></returns> public static String SerializeToXmlUtf8(this object source) { String result; try { var serializer = new XmlSerializer(source.GetType()); var settings = new XmlWriterSettings { Encoding = new UnicodeEncoding(false, false), // QQQ Encoding.UTF8 Indent = true, OmitXmlDeclaration = false }; using (var stringWriter = new StringWriteUtf8()) { using (var xmlWriter = XmlWriter.Create(stringWriter, settings)) { serializer.Serialize(xmlWriter, source); result = stringWriter.ToString(); } } } catch (Exception ex) { result = String.Format("Unable to serialize object of type {0}, message: {1}", source.GetType().FullName, ex.Message); } return(result); }
/// <summary> /// XmlSerialize the specified list of strings. /// </summary> /// <param name="source"></param> /// <returns></returns> public static String Serialize(List <String> source) { var serializer = new XmlSerializer(typeof(List <String>)); var settings = new XmlWriterSettings { Encoding = new UnicodeEncoding(false, false), Indent = false, OmitXmlDeclaration = true, NewLineHandling = NewLineHandling.None, }; using (var stringWriter = new StringWriteUtf8()) { using (var xmlWriter = XmlWriter.Create(stringWriter, settings)) { serializer.Serialize(xmlWriter, source ?? new List <String>(0)); var t1 = stringWriter.ToString(); var serialized = t1.Replace(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", ""); return(serialized); } } }