DataView to XML

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();
 
G

Guest

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.
 
S

Stuart Shay

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 */
}
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top