/// <summary> /// Serializes the object to an XML string with custom formatting/processing settings /// and store the result directly in the result store. /// </summary> /// <param name="obj">Object to serialize.</param> /// <param name="options">Options to modify the string output behaviour.</param> /// <param name="resultStore">Write cache for the XML result.</param> public static void Serialize(object obj, XmlOptions options, StringBuilder resultStore) { options.ThrowIfNull(nameof(options)); resultStore.ThrowIfNull(nameof(resultStore)); Serialize(obj, options, new StringWriter(resultStore)); }
static XmlProcessor() { defaultOptions = new XmlOptions(); defaultOptions.SerializationDefinition = new XmlSerializationDefinition(); defaultOptions.Encoding = Encoding.UTF8; defaultOptions.CompactOutput = true; }
/// <summary> /// Serializes the object to an XML string with custom formatting/processing settings. /// </summary> /// <param name="obj">Object to serialize.</param> /// <param name="options">Options to modify the string output behaviour.</param> /// <returns>An XML representation of the given object.</returns> public static string Serialize(object obj, XmlOptions options) { options.ThrowIfNull(nameof(options)); StringWriter resultStore = new StringWriter(); Serialize(obj, options, resultStore); return(resultStore.ToString()); }
/// <summary> /// Serializes the object to an XML string with custom formatting/processing settings /// and store the result directly in the result store. /// </summary> /// <param name="obj">Object to serialize.</param> /// <param name="options">Options to modify the string output behaviour.</param> /// <param name="resultStore">Write cache for the XML result.</param> public static void Serialize(object obj, XmlOptions options, TextWriter resultStore) { options.ThrowIfNull(nameof(options)); resultStore.ThrowIfNull(nameof(resultStore)); XmlSerializationDefinition definition = (options.SerializationDefinition != null) ? options.SerializationDefinition : defaultOptions.SerializationDefinition; XDocument document = ToXml(obj, definition); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.Indent = !options.CompactOutput; writerSettings.OmitXmlDeclaration = options.HideHeader; writerSettings.Encoding = options.Encoding; using (XmlWriter writer = XmlWriter.Create(resultStore, writerSettings)) { document.Save(writer); } }