I wrote a simple implementation of reading files. You can easily adapt this to read an XML file:
public class MyFileDataReader : IDataReader { protected StreamReader Stream { get; set; } protected object[] Values; protected bool Eof { get; set; } protected string CurrentRecord { get; set; } protected int CurrentIndex { get; set; } public MyFileDataReader(string fileName) { Stream = new StreamReader(fileName); Values = new object[this.FieldCount]; }
Remember that IDataReader has several methods that you do not need to perform depending on your scenario. But there are probably some implementation methods that you cannot avoid:
public void Close() { Array.Clear(Values, 0, Values.Length); Stream.Close(); Stream.Dispose(); } public int Depth { get { return 0; } } public DataTable GetSchemaTable() {
To implement IDataReader, you must also implement the IDisposable and IDataRecord interfaces.
IDisposable is simple, but IDataRecord can be painful. Again, in this scenario there are some implementation methods that we cannot avoid:
public int FieldCount { get { return 3;//assuming the table has 3 columns } } public IDataReader GetData(int i) { if (i == 0) return this; return null; } public string GetDataTypeName(int i) { return "String"; } public string GetName(int i) { return Values[i].ToString(); } public string GetString(int i) { return Values[i].ToString(); } public object GetValue(int i) { return Values[i]; } public int GetValues(object[] values) { Fill(values); Array.Copy(values, Values, this.FieldCount); return this.FieldCount; } public object this[int i] { get { return Values[i]; } }
Hope this helps.
playful
source share