Table of Contents

Class StreamExtensions

Namespace
Cuemon.Extensions.Xml
Assembly
Cuemon.Extensions.Xml.dll

Extension methods for the Stream class.

public static class StreamExtensions
Inheritance
StreamExtensions

Examples

StreamExtensions in the Xml namespace provides stream-based XML operations including XmlReader creation, indented copying, encoding detection, and namespace removal. This example loads an XML string into a MemoryStream and demonstrates four operations: ToXmlReader to parse element names, CopyXmlStream with Indent = true for pretty-printed XML output, TryDetectXmlEncoding to identify UTF-8 encoding, and RemoveXmlNamespaceDeclarations to strip namespace prefixes from elements. Each operation repositions the stream to Position = 0 before proceeding. Console output shows element names (root, item), the indented XML, the detected encoding name (Unicode (UTF-8)), and the namespace-cleaned content.

using System;
using System.IO;
using System.Text;
using System.Xml;
using Cuemon.Extensions.Xml;

namespace MyApp.Xml
{
    public class StreamExtensionsExample
    {
        public void Demonstrate()
        {
            var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><item>Value</item></root>";
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));

            // Convert an XML stream to an XmlReader
            using (var reader = stream.ToXmlReader())
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        Console.WriteLine(reader.Name);

            // Reset stream position for next demonstration
            stream.Position = 0;

            // Copy an XML stream with specified writer settings (e.g., indented output)
            using (var copy = stream.CopyXmlStream(o => o.Indent = true))
            {
                var indentedXml = new StreamReader(copy).ReadToEnd();
                Console.WriteLine(indentedXml); // Indented XML

            stream.Position = 0;

            // Detect XML encoding from the stream
            if (stream.TryDetectXmlEncoding(out var encoding))
            {
                Console.WriteLine(encoding.EncodingName); // "Unicode (UTF-8)"

            stream.Position = 0;

            // Remove XML namespace declarations from the stream
            using (var noNs = stream.RemoveXmlNamespaceDeclarations())
            {
                var cleaned = new StreamReader(noNs).ReadToEnd();
                Console.WriteLine(cleaned); // Elements without namespace prefixes

            stream.Dispose();

}}}}}}}}
}

Methods

CopyXmlStream(Stream, Action<XmlWriterSettings>)

Copies the entire XML Stream following the output format of setup.

public static Stream CopyXmlStream(this Stream value, Action<XmlWriterSettings> setup = null)

Parameters

value Stream

The XML Stream to extend.

setup Action<XmlWriterSettings>

The XmlWriterSettings which may be configured.

Returns

Stream

A Stream that is equivalent to value following the output format of setup.

Exceptions

ArgumentNullException

value cannot be null.

RemoveXmlNamespaceDeclarations(Stream, Action<XmlWriterSettings>)

Remove the XML namespace declarations from the specified value.

public static Stream RemoveXmlNamespaceDeclarations(this Stream value, Action<XmlWriterSettings> setup = null)

Parameters

value Stream

The XML Stream to extend.

setup Action<XmlWriterSettings>

The XmlWriterSettings which may be configured.

Returns

Stream

A Stream object representing the specified value, but with no namespace declarations.

ToXmlReader(Stream, Encoding, Action<XmlReaderSettings>)

Converts the given value to an XmlReader.

public static XmlReader ToXmlReader(this Stream value, Encoding encoding = null, Action<XmlReaderSettings> setup = null)

Parameters

value Stream

The XML Stream to extend.

encoding Encoding

The text encoding to use.

setup Action<XmlReaderSettings>

The XmlReaderSettings which may be configured.

Returns

XmlReader

An XmlReader representation of value.

Remarks

If encoding is null, an Encoding object will be attempted resolved by TryDetectXmlEncoding(Stream, out Encoding).

Exceptions

ArgumentNullException

value cannot be null.

TryDetectXmlEncoding(Stream, out Encoding)

Tries to resolve the Encoding level of the XML document from the Stream.

public static bool TryDetectXmlEncoding(this Stream value, out Encoding result)

Parameters

value Stream

The XML Stream to extend.

result Encoding

When this method returns, it contains the Encoding value equivalent to the encoding level of the XML document contained in value, if the conversion succeeded, or a null reference (Nothing in Visual Basic) if the conversion failed. The conversion fails if the value parameter is null, does not contain BOM information or does not contain an XmlDeclaration.

Returns

bool

true if the value parameter was converted successfully; otherwise, false.

Exceptions

ArgumentNullException

value cannot be null.