Beispiel #1
0
        /// <summary>
        /// Output this <see cref="XDocument"/> to a <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">
        /// The <see cref="Stream"/> to output the XML to.
        /// </param>
        /// <param name="options">
        /// If SaveOptions.DisableFormatting is enabled the output is not indented.
        /// If SaveOptions.OmitDuplicateNamespaces is enabled duplicate namespace declarations will be removed.
        /// </param>
        /// <param name="cancellationToken">A cancellation token.</param>
        public async Task SaveAsync(Stream stream, SaveOptions options, CancellationToken cancellationToken)
        {
            XmlWriterSettings ws = GetXmlWriterSettings(options);

            ws.Async = true;

            if (_declaration != null && !string.IsNullOrEmpty(_declaration.Encoding))
            {
                try
                {
                    ws.Encoding = Encoding.GetEncoding(_declaration.Encoding);
                }
                catch (ArgumentException)
                {
                }
            }

            XmlWriter w = XmlWriter.Create(stream, ws);

            await using (w.ConfigureAwait(false))
            {
                await WriteToAsync(w, cancellationToken).ConfigureAwait(false);

                await w.FlushAsync().ConfigureAwait(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Output this <see cref="XDocument"/> to a <see cref="TextWriter"/>.
        /// </summary>
        /// <param name="textWriter">
        /// The <see cref="TextWriter"/> to output the XML to.
        /// </param>
        /// <param name="options">
        /// If SaveOptions.DisableFormatting is enabled the output is not indented.
        /// If SaveOptions.OmitDuplicateNamespaces is enabled duplicate namespace declarations will be removed.
        /// </param>
        /// <param name="cancellationToken">A cancellation token.</param>
        public async Task SaveAsync(TextWriter textWriter, SaveOptions options, CancellationToken cancellationToken)
        {
            XmlWriterSettings ws = GetXmlWriterSettings(options);

            ws.Async = true;

            XmlWriter w = XmlWriter.Create(textWriter, ws);

            await using (w.ConfigureAwait(false))
            {
                await WriteToAsync(w, cancellationToken).ConfigureAwait(false);

                await w.FlushAsync().ConfigureAwait(false);
            }
        }