Table of Contents

StreamDecoratorExtensions Class

Definition

Namespace
Cuemon.Xml
Assemblies
Cuemon.Xml.dll
Source
src/Cuemon.Xml/Extensions/StreamDecoratorExtensions.cs

Extension methods for the Stream class hidden behind the IDecorator<T> interface.

public static class StreamDecoratorExtensions
Inheritance
object
StreamDecoratorExtensions

Examples

The following example demonstrates how to create an from a stream and detect the XML encoding.

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

namespace MyApp.Examples;

public static class StreamDecoratorExtensionsExample
{
    public static void Demonstrate()
    {
        const string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root><child>hello</child></root>";

        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
        using XmlReader reader = Decorator.Enclose(stream).ToXmlReader();
        reader.MoveToContent();

        Console.WriteLine(reader.LocalName);

        using var detectStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
        var detected = Decorator.Enclose(detectStream).TryDetectXmlEncoding(out var encoding);

        Console.WriteLine(detected);
        Console.WriteLine(encoding.EncodingName);
    }
}

Methods

Name Description
ToXmlReader(IDecorator<Stream>, Encoding, Action<XmlReaderSettings>)

Converts the enclosed Stream of the specified decorator to an XmlReader.

TryDetectXmlEncoding(IDecorator<Stream>, out Encoding)

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

See Also