/// <summary> /// Converts the given stream to an XmlReader object. /// </summary> /// <param name="value">The stream to be converted.</param> /// <param name="encoding">The text encoding to use.</param> /// <param name="setup">The <see cref="XmlReaderSettings"/> which need to be configured.</param> /// <returns>An <see cref="System.Xml.XmlReader"/> object.</returns> /// <remarks>If <paramref name="encoding"/> is null, an <see cref="Encoding"/> object will be attempted resolved by <see cref="XmlEncodingUtility.ReadEncoding(Stream)"/>.</remarks> public static XmlReader FromStream(Stream value, Encoding encoding, Action <XmlReaderSettings> setup = null) { Validator.ThrowIfNull(value, nameof(value)); if (encoding == null) { encoding = XmlEncodingUtility.ReadEncoding(value); } if (value.CanSeek) { value.Position = 0; } var options = setup.ConfigureOptions(); XmlReader reader = XmlReader.Create(new StreamReader(value, encoding), options); return(reader); }
/// <summary> /// Converts the entire XML <see cref="Stream"/> object from the resolved encoding of <paramref name="source"/> to the specified encoding. /// If an encoding cannot be resolved from <paramref name="source"/>, UTF-8 encoding is assumed. /// </summary> /// <param name="source">The <see cref="Stream"/> to apply the conversion to.</param> /// <param name="targetEncoding">The target encoding format.</param> /// <param name="omitXmlDeclaration">if set to <c>true</c> omit the XML declaration; otherwise <c>false</c>. The default is false.</param> /// <returns>A <see cref="Stream"/> object containing the results of converting bytes from the resolved source encoding to the specified targetEncoding.</returns> public static Stream ChangeEncoding(Stream source, Encoding targetEncoding, bool omitXmlDeclaration) { return(ChangeEncoding(source, XmlEncodingUtility.ReadEncoding(source), targetEncoding, PreambleSequence.Keep, omitXmlDeclaration)); }
/// <summary> /// Converts the entire XML <see cref="Stream"/> object from the resolved encoding of <paramref name="source"/> to the specified encoding. /// If an encoding cannot be resolved from <paramref name="source"/>, UTF-8 encoding is assumed. /// </summary> /// <param name="source">The <see cref="Stream"/> to apply the conversion to.</param> /// <param name="targetEncoding">The target encoding format.</param> /// <param name="sequence">Determines whether too keep or remove any preamble sequences.</param> /// <returns>A <see cref="Stream"/> object containing the results of converting bytes from the resolved source encoding to the specified targetEncoding.</returns> public static Stream ChangeEncoding(Stream source, Encoding targetEncoding, PreambleSequence sequence) { return(ChangeEncoding(source, XmlEncodingUtility.ReadEncoding(source), targetEncoding, sequence)); }