XmlDataReader Class
Definition
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
| Name | Description |
|---|---|
| XmlDataReader(XmlReader, Func<string, object>, Action<FormattingOptions>) | Initializes a new instance of the XmlDataReader class. |
Properties
| Name | Description |
|---|---|
| Depth | Gets a value indicating the depth of nesting for the current element. |
| NullRead | Gets the value that indicates that no more rows exists. |
| RowCount | Gets the currently processed row count of this instance. |
Methods
| Name | Description |
|---|---|
| OnDisposeManagedResources() | Called when this object is being disposed by either Dispose() or Dispose(bool) having |
| Read() | Advances this instance to the next element of the XML data source. |
| ReadNext(bool) | Advances this instance to the next element of the XML data source. |