Class XmlSingleStreamExtractor<TRecord>
Extracts items of type TRecord from a single XML stream
containing a root element with child elements.
public sealed class XmlSingleStreamExtractor<TRecord> : ExtractorBase<TRecord, XmlReport>, IExtractWithProgressAndCancellationAsync<TRecord, XmlReport>, IExtractWithCancellationAsync<TRecord>, IExtractWithProgressAsync<TRecord, XmlReport>, IExtractAsync<TRecord>, IReportsItemErrors, IAsyncDisposable, IDisposable where TRecord : notnull, new()
Type Parameters
TRecordThe type of items to extract. Must be
notnulland have a parameterless constructor.
- Inheritance
-
ExtractorBase<TRecord, XmlReport>XmlSingleStreamExtractor<TRecord>
- Implements
-
IExtractWithProgressAndCancellationAsync<TRecord, XmlReport>IExtractWithCancellationAsync<TRecord>IExtractWithProgressAsync<TRecord, XmlReport>IExtractAsync<TRecord>IReportsItemErrors
- Inherited Members
-
ExtractorBase<TRecord, XmlReport>.ExtractAsync()ExtractorBase<TRecord, XmlReport>.DisposeAsync()ExtractorBase<TRecord, XmlReport>.Dispose()ExtractorBase<TRecord, XmlReport>.ReportingIntervalExtractorBase<TRecord, XmlReport>.CurrentItemCountExtractorBase<TRecord, XmlReport>.CurrentSkippedItemCountExtractorBase<TRecord, XmlReport>.CurrentErrorItemCountExtractorBase<TRecord, XmlReport>.MaximumItemCountExtractorBase<TRecord, XmlReport>.SkipItemCountExtractorBase<TRecord, XmlReport>.WorkerResilienceExtractorBase<TRecord, XmlReport>.ErrorPolicy
Examples
// Leave stream open (default) — caller controls stream lifetime:
using var stream = File.OpenRead("data.xml");
var extractor = new XmlSingleStreamExtractor<Person>(stream);
await foreach (var person in extractor.ExtractAsync(cancellationToken))
{
Console.WriteLine(person.Name);
}
// Transfer stream ownership — closed automatically when extraction completes:
var owningExtractor = new XmlSingleStreamExtractor<Person>
(
File.OpenRead("data.xml"),
new XmlSingleStreamExtractorOptions { LeaveOpen = false }
);
await foreach (var person in owningExtractor.ExtractAsync(cancellationToken))
{
Console.WriteLine(person.Name);
}
Remarks
Reads an XML document (e.g. <ArrayOfPerson><Person/>...</ArrayOfPerson>)
from a Stream and yields each deserialized child element as an item
in the async enumerable sequence. Uses XmlReader for streaming deserialization
so that the entire document is not buffered in memory.
By default the stream is left open after extraction completes. To have the stream closed
automatically when extraction finishes, set LeaveOpen
to false, mirroring the behaviour of StreamReader and
BinaryReader.
Constructors
XmlSingleStreamExtractor(Stream, ILogger<XmlSingleStreamExtractor<TRecord>>)
Initializes a new instance of the XmlSingleStreamExtractor<TRecord> class with a logger.
[RequiresUnreferencedCode("XmlSingleStreamExtractor deserializes TRecord via System.Xml.Serialization.XmlSerializer, which uses runtime reflection/Reflection.Emit the trimmer cannot follow. The library is not trim/NativeAOT safe.")]
public XmlSingleStreamExtractor(Stream stream, ILogger<XmlSingleStreamExtractor<TRecord>> logger)
Parameters
streamStreamThe stream containing XML data to read from.
loggerILogger<XmlSingleStreamExtractor<TRecord>>The logger instance for diagnostic output.
Exceptions
- ArgumentNullException
Thrown when
streamorloggerisnull.
XmlSingleStreamExtractor(Stream, XmlReaderSettings, ILogger<XmlSingleStreamExtractor<TRecord>>, XmlSingleStreamExtractorOptions?)
Initializes a new instance of the XmlSingleStreamExtractor<TRecord> class with custom reader settings.
[RequiresUnreferencedCode("XmlSingleStreamExtractor deserializes TRecord via System.Xml.Serialization.XmlSerializer, which uses runtime reflection/Reflection.Emit the trimmer cannot follow. The library is not trim/NativeAOT safe.")]
public XmlSingleStreamExtractor(Stream stream, XmlReaderSettings readerSettings, ILogger<XmlSingleStreamExtractor<TRecord>> logger, XmlSingleStreamExtractorOptions? options = null)
Parameters
streamStreamThe stream containing XML data to read from.
readerSettingsXmlReaderSettingsThe XML reader settings to use for deserialization.
loggerILogger<XmlSingleStreamExtractor<TRecord>>The logger instance for diagnostic output.
optionsXmlSingleStreamExtractorOptionsOptions that control extractor behaviour. When
null, defaults are used.
Exceptions
- ArgumentNullException
Thrown when
stream,readerSettings, orloggerisnull.
XmlSingleStreamExtractor(Stream, XmlSingleStreamExtractorOptions?)
Initializes a new instance of the XmlSingleStreamExtractor<TRecord> class.
[RequiresUnreferencedCode("XmlSingleStreamExtractor deserializes TRecord via System.Xml.Serialization.XmlSerializer, which uses runtime reflection/Reflection.Emit the trimmer cannot follow. The library is not trim/NativeAOT safe.")]
public XmlSingleStreamExtractor(Stream stream, XmlSingleStreamExtractorOptions? options = null)
Parameters
streamStreamThe stream containing XML data to read from.
optionsXmlSingleStreamExtractorOptionsOptions that control extractor behaviour. When
null, defaults are used.
Exceptions
- ArgumentNullException
Thrown when
streamisnull.
XmlSingleStreamExtractor(Stream, XmlSingleStreamExtractorOptions?, ILogger<XmlSingleStreamExtractor<TRecord>>?)
Initializes a new instance of the XmlSingleStreamExtractor<TRecord> class.
This is the canonical constructor — every other overload delegates to it — and it is the
only one that lets options and logger be supplied
together without also supplying XmlReaderSettings.
[RequiresUnreferencedCode("XmlSingleStreamExtractor deserializes TRecord via System.Xml.Serialization.XmlSerializer, which uses runtime reflection/Reflection.Emit the trimmer cannot follow. The library is not trim/NativeAOT safe.")]
public XmlSingleStreamExtractor(Stream stream, XmlSingleStreamExtractorOptions? options, ILogger<XmlSingleStreamExtractor<TRecord>>? logger = null)
Parameters
streamStreamThe stream containing XML data to read from.
optionsXmlSingleStreamExtractorOptionsOptions that control extractor behaviour. When
null, defaults are used.loggerILogger<XmlSingleStreamExtractor<TRecord>>An optional logger instance for diagnostic output. When
null, logging is disabled.
Examples
// Options and a logger, without having to supply XmlReaderSettings:
var extractor = new XmlSingleStreamExtractor<Person>
(
stream,
options: new XmlSingleStreamExtractorOptions { LeaveOpen = false },
logger: loggerFactory.CreateLogger<XmlSingleStreamExtractor<Person>>()
);
Exceptions
- ArgumentNullException
Thrown when
streamisnull.
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 extraction 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.
ExtractWorkerAsync(CancellationToken)
This method is the core implementation of the extraction logic and should be overridden by derived classes.
protected override IAsyncEnumerable<TRecord> ExtractWorkerAsync(CancellationToken token)
Parameters
tokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
- IAsyncEnumerable<TRecord>
IAsyncEnumerable<TSource> The result may be an empty sequence if no data is available or if the extraction fails.