Class XmlDataReader
Provides a way of reading a forward-only stream of rows from an XML based data source. This class cannot be inherited.
public sealed class XmlDataReader : DataReader<bool>, IDataReader, IDataRecord, IDisposable
- Inheritance
-
XmlDataReader
- Implements
- Inherited Members
Examples
The following example demonstrates how to read XML data as a tabular result set using XmlDataReader. It iterates through records, accesses fields by name, and prints row metadata such as depth and field count.
using System;
using System.IO;
using System.Xml;
using Cuemon.Data.Xml;
namespace MyApp.Data
{
public class XmlDataReaderExample
{
public void Demonstrate()
{
// Create XML data to read
var xml = @"<records>
<record><id>1</id><name>Alice</name><score>95.5</score></record>
<record><id>2</id><name>Bob</name><score>87.0</score></record>
<record><id>3</id><name>Charlie</name><score>92.3</score></record>
</records>";
using var stringReader = new StringReader(xml);
using var xmlReader = XmlReader.Create(stringReader);
// Create the XmlDataReader
using var dataReader = new XmlDataReader(xmlReader);
// Read through the records like a database result set
while (dataReader.Read())
{
Console.WriteLine($"Row {dataReader.RowCount}:");
Console.WriteLine($" Id: {dataReader["id"]}");
Console.WriteLine($" Name: {dataReader["name"]}");
Console.WriteLine($" Score: {dataReader["score"]}");
Console.WriteLine($" Depth: {dataReader.Depth}");
}
Console.WriteLine($"Total rows read: {dataReader.RowCount}");
// Verify field count
Console.WriteLine($"Fields per row: {dataReader.FieldCount}");
}
}
}
Constructors
XmlDataReader(XmlReader, Func<string, object>, Action<FormattingOptions>)
Initializes a new instance of the XmlDataReader class.
public XmlDataReader(XmlReader reader, Func<string, object> parser = null, Action<FormattingOptions> setup = null)
Parameters
readerXmlReaderThe XmlReader object that contains the XML data.
parserFunc<string, object>The function delegate that returns a primitive object whose value is equivalent to the provided string value. Default is FromValueType().
setupAction<FormattingOptions>The FormattingOptions which may be configured.
Exceptions
- ArgumentNullException
readeris null.
Properties
Depth
Gets a value indicating the depth of nesting for the current element.
public override int Depth { get; }
Property Value
- int
The level of nesting.
NullRead
Gets the value that indicates that no more rows exists.
protected override bool NullRead { get; }
Property Value
- bool
The value that indicates that no more rows exists.
RowCount
Gets the currently processed row count of this instance.
public override int RowCount { get; protected set; }
Property Value
- int
The currently processed row count of this instance.
Remarks
This property is incremented when the invoked Read() method returns true.
Methods
OnDisposeManagedResources()
Called when this object is being disposed by either Dispose() or Dispose(bool) having disposing set to true and Disposed is false.
protected override void OnDisposeManagedResources()
Read()
Advances this instance to the next element of the XML data source.
public override bool Read()
Returns
- bool
trueif there are more elements; otherwise,false.
Exceptions
- ObjectDisposedException
This instance has been disposed.
ReadNext(bool)
Advances this instance to the next element of the XML data source.
protected override bool ReadNext(bool columns)
Parameters
columnsbool
Returns
- bool
trueif there are more elements; otherwise,false.
Exceptions
- ObjectDisposedException
This instance has been disposed.