Reputation: 319
I have created a DataSet object from an XML file.
DataSet ds = new DataSet();
using (StreamReader sr = new StreamReader(file))
{
XmlTextReader xmltext = new XmlTextReader(sr);
ds = new DataSet();
ds.ReadXml(xmltext);
}
Then I create a DataTableReader object and start to loop through each table and each row.
using (DataTableReader dtr = ds.CreateDataReader())
{
while (dtr.NextResult()) // foreach table
{
// would like to access table name here
while (dtr.Read()) // foreach row
{
....
My problem is that I would like access the name of each table in the DataTableReader, but there doesn't seem to be a property available for me to use.
If I inspect the DataTableReader object I see the table name is stored in a property called "CurrentDataTable", but this is a non-public member and I cannot access it.
I have also tried using the 'GetSchemaTable' function on the DataTableReader which returns a DataTable object. This seemed promising at first, but the 'TableName' property of the DataTable object has a value of "SchemaTable" which is not correct.
Upvotes: 1
Views: 247
Reputation: 319
So I was able to find a work around. There are some other StackOverflow answers I found which suggested using reflection to access the non-public member.
This code allowed me to access the string I was seeking.
PropertyInfo pInfo = dtr.GetType()
.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
.Single(x => x.Name == "CurrentDataTable");
pInfo.GetValue(dtr, null).ToString();
While this is technically working it feels like a very ham-fisted way to accomplish something that seems so trivial.
Surely there is a better way?
Upvotes: 1