DataView to XML

  • Thread starter Thread starter Stuart Shay
  • Start date Start date
S

Stuart Shay

Hello All:

I want to Filter a DataView then output to XML, the problem is when I place a Row Filter on the DataView the changes are not applied on the XML output.

Thanks
Stuart


DataView oDataView = new DataView(oDataSet.Tables[0]);
oDataView.RowFilter = string.Format("EmployeeID = '{0}'", "2");

// Count is Filtering Correctly
Response.Write("Count" + oDataView.Count + "<br/>");

// Not Filtering
string xmlString = oDataView.Table.DataSet.GetXml();
 
string xmlString = oDataView.ToTable().DataSet.GetXml();

ToTable() creates a datatable based on the current dataview. This should
give you what you are looking for.

G'luck.
 
Travis:

I tried the code and I got "Object Reference not set to a Instance of a Object" the dataview is filtering correctly since the count is properly display

I went ahead and looped through the filtered DataRows of the DataSet and then created the XML using the XMLTextWriter as show in the code below.

Thanks
Stuart

oDataSet = ec.EmployeesTerritory();

string filter = string.Format("EmployeeID = '{0}'", "4");

DataRow[] oDataRow;
oDataRow = oDataSet.Tables[0].Select(filter);
foreach (DataRow row in oDataRow)
{
/* Use XmlTextWriter Here */
}
 
Stuart,

I should have checked the code prior to submitting it...I assumed
(incorrectly) that the dataset would not be null.

However, the following should work (at least in my sample code)
DataSet ds = new DataSet();
ds.Tables.Add(oDataView.ToTable());
string xmlString = ds.GetXml();

Apologies for the initial misleading post.
 
Back
Top