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.
// Program.cs
using System;
using System.IO;
using System.Xml;
using Cuemon.Data.Xml;
// Create XML data to read (attributes on each record element form one tabular row)
var xml = @"<records>
<record id=""1"" name=""Alice"" score=""95.5"" />
<record id=""2"" name=""Bob"" score=""87.0"" />
<record id=""3"" name=""Charlie"" score=""92.3"" />
</records>";
using var stringReader = new StringReader(xml);
using var xmlReader = XmlReader.Create(stringReader);
// Create the XmlDataReader
using var dataReader = new XmlDataReader(xmlReader);
int fieldCount = 0;
// Read through the records like a database result set
while (dataReader.Read())
{
fieldCount = dataReader.FieldCount;
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 captured while reading rows
Console.WriteLine($"Fields per row: {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. |