Class XmlSingleStreamLoader<TRecord>
Loads items of type TRecord into a single XML stream
wrapped in a root element.
public sealed class XmlSingleStreamLoader<TRecord> : LoaderBase<TRecord, XmlReport>, ILoadWithProgressAndCancellationAsync<TRecord, XmlReport>, ILoadWithProgressAsync<TRecord, XmlReport>, ILoadWithCancellationAsync<TRecord>, ILoadAsync<TRecord> where TRecord : notnull, new()
Type Parameters
TRecordThe type of items to load. Must be
notnulland have a parameterless constructor.
- Inheritance
-
LoaderBase<TRecord, XmlReport>XmlSingleStreamLoader<TRecord>
- Implements
-
ILoadWithProgressAndCancellationAsync<TRecord, XmlReport>ILoadWithProgressAsync<TRecord, XmlReport>ILoadWithCancellationAsync<TRecord>ILoadAsync<TRecord>
- Inherited Members
-
LoaderBase<TRecord, XmlReport>.ReportingIntervalLoaderBase<TRecord, XmlReport>.CurrentItemCountLoaderBase<TRecord, XmlReport>.CurrentSkippedItemCountLoaderBase<TRecord, XmlReport>.MaximumItemCountLoaderBase<TRecord, XmlReport>.SkipItemCount
Examples
// Default root element name (ArrayOfPerson), stream left open:
using var stream = File.Create("output.xml");
var loader = new XmlSingleStreamLoader<Person>(stream);
await loader.LoadAsync(items, cancellationToken);
// Custom root element name, stream closed automatically:
var loader = new XmlSingleStreamLoader<Person>
(
File.Create("output.xml"),
new XmlSingleStreamLoaderOptions
{
RootElementName = "People",
LeaveOpen = false,
}
);
await loader.LoadAsync(items, cancellationToken);
Remarks
Writes an XML document to a Stream by serializing each item from the input
async enumerable sequence as a child element of a configurable root element. The root
element name defaults to ArrayOf{TypeName} (e.g. ArrayOfPerson) but can be
overridden via RootElementName.
Each item is serialized using XmlSerializer.
By default the stream is left open after loading completes. To have the stream closed
automatically when loading finishes, set LeaveOpen
to false, mirroring the behaviour of StreamWriter and
BinaryWriter.
Constructors
XmlSingleStreamLoader(Stream, ILogger<XmlSingleStreamLoader<TRecord>>)
Initializes a new instance of the XmlSingleStreamLoader<TRecord> class with a logger.
public XmlSingleStreamLoader(Stream stream, ILogger<XmlSingleStreamLoader<TRecord>> logger)
Parameters
streamStreamThe stream to write XML data to.
loggerILogger<XmlSingleStreamLoader<TRecord>>The logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamorloggerisnull.
XmlSingleStreamLoader(Stream, XmlWriterSettings, ILogger<XmlSingleStreamLoader<TRecord>>, XmlSingleStreamLoaderOptions?)
Initializes a new instance of the XmlSingleStreamLoader<TRecord> class with custom writer settings.
public XmlSingleStreamLoader(Stream stream, XmlWriterSettings writerSettings, ILogger<XmlSingleStreamLoader<TRecord>> logger, XmlSingleStreamLoaderOptions? options = null)
Parameters
streamStreamThe stream to write XML data to.
writerSettingsXmlWriterSettingsThe XML writer settings to use for serialization.
loggerILogger<XmlSingleStreamLoader<TRecord>>The logger instance for diagnostic output.
optionsXmlSingleStreamLoaderOptionsOptions that control loader behaviour. When
null, defaults are used.
Exceptions
- ArgumentNullException
Thrown when
stream,writerSettings, orloggerisnull.- ArgumentException
Thrown when RootElementName is an empty or whitespace string.
XmlSingleStreamLoader(Stream, XmlSingleStreamLoaderOptions?)
Initializes a new instance of the XmlSingleStreamLoader<TRecord> class.
public XmlSingleStreamLoader(Stream stream, XmlSingleStreamLoaderOptions? options = null)
Parameters
streamStreamThe stream to write XML data to.
optionsXmlSingleStreamLoaderOptionsOptions that control loader behaviour. When
null, defaults are used.
Exceptions
- ArgumentNullException
Thrown when
streamisnull.- ArgumentException
Thrown when RootElementName is an empty or whitespace string.
Methods
CreateProgressReport()
Creates a progress report of type TProgress. This gives the derived class the opportunity to implement a custom progress report that is specific to the loading process.
protected override XmlReport CreateProgressReport()
Returns
- XmlReport
Progress of type TProgress
CreateProgressTimer(IProgress<XmlReport>)
Creates the Wolfgang.Etl.Abstractions.IProgressTimer used to drive progress callbacks. Override this method in a derived class to inject a custom timer (for example, a custom implementation that allows manual control in unit tests).
protected override IProgressTimer CreateProgressTimer(IProgress<XmlReport> progress)
Parameters
Returns
- IProgressTimer
A started Wolfgang.Etl.Abstractions.IProgressTimer instance.
LoadWorkerAsync(IAsyncEnumerable<TRecord>, CancellationToken)
This method is the core implementation of the loading logic and should be overridden by derived classes.
protected override Task LoadWorkerAsync(IAsyncEnumerable<TRecord> items, CancellationToken token)
Parameters
itemsIAsyncEnumerable<TRecord>The items to be loaded to the destination.
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- Task
A task representing the asynchronous operation.
Remarks
Items may be an empty sequence if no data is available or if the loading fails.
Exceptions
- ArgumentNullException
Argument items is null