public string GetXml() { var settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, IndentChars = "\t", NewLineChars = "\r\n", NewLineHandling = NewLineHandling.Replace, ConformanceLevel = ConformanceLevel.Document }; string result; using (StringWriter sw = new CustomEncodingStringWriter(Encoding.UTF8)) { using (var xw = XmlWriter.Create(sw, settings)) { _xDocument.Save(xw); } result = sw.ToString(); } return(result); }
public static string ToXmlString(Action <XmlWriter> action, bool indent) { var sb = new StringBuilder(); using (var stringWriter = new CustomEncodingStringWriter(sb, null)) using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = indent })) { action(writer); } return(sb.ToString()); }
public static async Task <string> XmbToXmlAsync(byte[] data) { using (var fileStream = new MemoryStream(data, false)) { XMBFile xmb = await XMBFile.LoadXMBFile(fileStream); using StringWriter sw = new CustomEncodingStringWriter(Encoding.UTF8); using XmlTextWriter textWriter = new XmlTextWriter(sw); textWriter.Formatting = Formatting.Indented; xmb.file.Save(textWriter); return(sw.ToString()); } }
/// <summary> /// Serializes the specified System.Object and returns the serialized XML /// </summary> /// <typeparam name="T">This item's type</typeparam> /// <param name="item">This item</param> /// <param name="omitXmlDeclaration">False to keep the XML declaration. Otherwise, it will be removed.</param> /// <param name="removeNamespaces"> /// <para>Specify whether to remove xml namespaces.</para>para> /// <para>If your object has any XmlInclude attributes, then set this to false</para> /// </param> /// <param name="xmlns">If not null, "removeNamespaces" is ignored and the provided namespaces are used.</param> /// <param name="encoding">Specify encoding, if required.</param> /// <returns>Serialized XML for specified System.Object</returns> public static string XmlSerialize <T>( this T item, bool omitXmlDeclaration = true, bool removeNamespaces = true, XmlSerializerNamespaces xmlns = null, Encoding encoding = null) { object locker = new object(); var xmlSerializer = new XmlSerializer(item.GetType()); var settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = omitXmlDeclaration }; lock (locker) { var stringBuilder = new StringBuilder(); using (var stringWriter = new CustomEncodingStringWriter(encoding, stringBuilder)) { using (var xmlWriter = XmlWriter.Create(stringWriter, settings)) { if (xmlns != null) { xmlSerializer.Serialize(xmlWriter, item, xmlns); } else { if (removeNamespaces) { xmlns = new XmlSerializerNamespaces(); xmlns.Add(string.Empty, string.Empty); xmlSerializer.Serialize(xmlWriter, item, xmlns); } else { xmlSerializer.Serialize(xmlWriter, item); } } return(stringBuilder.ToString()); } } } }
public string Serialize(object obj, bool omitHeaderDeclaration = false) { var serializer = new XmlSerializer(obj.GetType()); var namespaces = new XmlSerializerNamespaces(); namespaces.Add("wps", ModelNamespaces.Wps); namespaces.Add("ows", ModelNamespaces.Ows); namespaces.Add("xli", ModelNamespaces.Xlink); namespaces.Add("xsi", ModelNamespaces.XmlSchemaInstance); var settings = new XmlWriterSettings { OmitXmlDeclaration = omitHeaderDeclaration }; using (var writer = new CustomEncodingStringWriter(Encoding.UTF8)) { using (var xmlWriter = XmlWriter.Create(writer, settings)) { serializer.Serialize(xmlWriter, obj, namespaces); return(writer.ToString()); } } }