Class XmlReaderDecoratorExtensions
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
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.
public static IEnumerable<XmlReader> Chunk(this IDecorator<XmlReader> decorator, int size = 128, Action<XmlWriterSettings> setup = null)
Parameters
decoratorIDecorator<XmlReader>The IDecorator{XmlReader} to extend.
sizeintThe amount of XML node elements allowed per XmlReader object. Default is 128 XML node element.
setupAction<XmlWriterSettings>The XmlWriterSettings which may be configured.
Returns
- IEnumerable<XmlReader>
An sequence of XmlReader instances that contains no more than the specified
sizeof XML node elements from the enclosed XmlReader of the specifieddecorator.
Exceptions
- ArgumentNullException
decoratoris null.- ArgumentException
The Read() method of the enclosed XmlReader of the specified
decoratorobject has already been called.
MoveToFirstElement(IDecorator<XmlReader>)
Moves the enclosed XmlReader of the specified decorator to the first element.
public static bool MoveToFirstElement(this IDecorator<XmlReader> decorator)
Parameters
decoratorIDecorator<XmlReader>The IDecorator{XmlReader} to extend.
Returns
- bool
trueif an element exists (the reader moves to the first element), otherwise,false(the reader has reached EOF).
Exceptions
- ArgumentNullException
decoratoris null.- ArgumentException
The Read() method of the enclosed XmlReader of the specified
decoratorobject has already been called.
ToHierarchy(IDecorator<XmlReader>)
Converts the XML hierarchy of the enclosed XmlReader of the specified decorator into an IHierarchy<T>.
public static IHierarchy<DataPair> ToHierarchy(this IDecorator<XmlReader> decorator)
Parameters
decoratorIDecorator<XmlReader>The IDecorator{XmlReader} to extend.
Returns
- IHierarchy<DataPair>
An IHierarchy{DataPair} implementation.
Exceptions
- ArgumentNullException
decoratorcannot be null.