/// <summary> /// Uses XML serialization to serialize an object. Flags may be specified to apply special behaviors. /// </summary> /// <remarks> /// The encoding attribute in the XML declaration will have the value UTF-8. You must take care /// to also specify UTF-8 encoding when you write the resulting XML string to any output stream(s). /// If you write the string to a non-UTF-8 output, encoding errors may occur during deserialization. /// </remarks> /// <exception cref="ArgumentNullException">Thrown if <paramref name="obj"/> is null.</exception> public static string XmlSerialize(this HelpersContainerClasses.XmlSerialization container, object obj, XmlSerializationFlags flags) { Helpers.Argument.ValidateIsNotNull(obj, nameof(obj)); var serializer = new XmlSerializer(obj.GetType()); using var stream = new MemoryStream(); var writer = XmlWriter.Create(stream, new XmlWriterSettings { Encoding = Encoding.UTF8, IndentChars = "\t", Indent = true }); if ((flags & XmlSerializationFlags.ClearNamespaceDefinitions) == XmlSerializationFlags.ClearNamespaceDefinitions) { var namespaces = new XmlSerializerNamespaces(); namespaces.Add("", ""); serializer.Serialize(writer, obj, namespaces); } else { serializer.Serialize(writer, obj); } writer.Flush(); stream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(stream, Encoding.UTF8); return(reader.ReadToEnd()); }
/// <summary> /// Uses XML serialization to deserialize an object. /// </summary> /// <remarks> /// The encoding attribute in the XML declaration will be ignored, if present. /// It is your responsibility to ensure that the XML is loaded into the string with the correct reader encoding. /// </remarks> /// <exception cref="ArgumentNullException">Thrown if <paramref name="xml"/> is null.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> is null.</exception> public static object?XmlDeserialize(this HelpersContainerClasses.XmlSerialization container, string xml, Type type) { Helpers.Argument.ValidateIsNotNull(xml, nameof(xml)); Helpers.Argument.ValidateIsNotNull(type, nameof(type)); var serializer = new XmlSerializer(type); using var reader = new StringReader(xml); return(serializer.Deserialize(reader)); }
/// <summary> /// Uses XML serialization to deserialize an object. /// </summary> /// <remarks> /// The encoding attribute in the XML declaration will be ignored, if present. /// It is your responsibility to ensure that the XML is loaded into the string with the correct reader encoding. /// </remarks> /// <exception cref="ArgumentNullException">Thrown if <paramref name="xml"/> is null.</exception> public static TObject?XmlDeserialize <TObject>(this HelpersContainerClasses.XmlSerialization container, string xml) { return((TObject?)Helpers.XmlSerialization.XmlDeserialize(xml, typeof(TObject))); }
/// <summary> /// Uses XML serialization to serialize an object. /// </summary> /// <remarks> /// The encoding attribute in the XML declaration will have the value UTF-8. You must take care /// to also specify UTF-8 encoding when you write the resulting XML string to any output stream(s). /// If you write the string to a non-UTF-8 output, encoding errors may occur during deserialization. /// </remarks> /// <exception cref="ArgumentNullException">Thrown if <paramref name="obj"/> is null.</exception> public static string XmlSerialize(this HelpersContainerClasses.XmlSerialization container, object obj) { return(Helpers.XmlSerialization.XmlSerialize(obj, XmlSerializationFlags.None)); }