Table of Contents

XmlReaderDecoratorExtensions Class

Definition

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

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

public static class XmlReaderDecoratorExtensions
Inheritance
XmlReaderDecoratorExtensions

Examples

The following example demonstrates how to move to the first XML element, chunk a document, and build a hierarchy from an .

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Cuemon;
using Cuemon.Xml;

namespace MyApp.Examples;

public static class XmlReaderDecoratorExtensionsExample
{
    public static void Demonstrate()
    {
        const string xml = "<items><item id=\"1\"/><item id=\"2\"/><item id=\"3\"/></items>";

        using var reader = XmlReader.Create(new StringReader(xml));
        var hasElement = Decorator.Enclose(reader).MoveToFirstElement();

        Console.WriteLine(hasElement);
        Console.WriteLine(reader.LocalName);

        using var chunkReader = XmlReader.Create(new StringReader(xml));
        var chunks = new List<XmlReader>(Decorator.Enclose(chunkReader).Chunk(size: 2));

        Console.WriteLine(chunks.Count);

        using var hierarchyReader = XmlReader.Create(new StringReader(xml));
        var hierarchy = Decorator.Enclose(hierarchyReader).ToHierarchy();

        Console.WriteLine(hierarchy.Instance.Name);
    }
}

Methods

Name Description
Chunk(IDecorator<XmlReader>, int, Action<XmlWriterSettings>)

Creates and returns a sequence of chunked XmlReader instances from the enclosed XmlReader of the specified decorator with a maximum of the specified size of XML node elements located on a depth of 1.

MoveToFirstElement(IDecorator<XmlReader>)

Moves the enclosed XmlReader of the specified decorator to the first element.

ToHierarchy(IDecorator<XmlReader>)

Converts the XML hierarchy of the enclosed XmlReader of the specified decorator into an IHierarchy<T>.

See Also