filtering an XML Dataset

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

I use the ReadXML method in my C# Windows Form project.
Here is some of my C# code:
xmlDS.ReadXml(curDir + "\\Dept.xml") ;

There're 20 rows and 5 columns in the Dept.xml.
But now I just want some of the data in the Dept.xml, e.g., DeptLevel >1 AND
Status=0.
How do I filter the XML or xmlDS so that the output is what I need?
Thanks for help.


Jason
 
You need to understand DataSets better, and the difference between a
serialized class and the class itself. The XML file you're talking about is
an XML document. When you create a DataSet and read from the file to
populate it, you are no longer dealing with XML; you're dealing with a
DataSet. An XML document doesn't have columns and rows; it has nodes. A
DataSet doesn't have columns and rows either; it has Tables (DataTables) and
schema information about the data and the tables. A DataTable can not be
filtered. But when you use a DataTable, you generally work with a DataView
rather then the table itself. Now, a DataView can be filtered, sorted, and
manipulated in a variety of ways. For more information, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconxmldataset.asp
http://msdn.microsoft.com/library/d...s/cpguide/html/cpconcreatingusingdatasets.asp
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingdatatables.asp?frame=true
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingusingdataviews.asp?frame=true

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
Jason Huang said:
Hi,

I use the ReadXML method in my C# Windows Form project.
Here is some of my C# code:
xmlDS.ReadXml(curDir + "\\Dept.xml") ;

There're 20 rows and 5 columns in the Dept.xml.
But now I just want some of the data in the Dept.xml, e.g., DeptLevel >1
AND Status=0.
How do I filter the XML or xmlDS so that the output is what I need?
Thanks for help.

Your best bet is probably just to use a DataView.

If you really want to filter the data coming into the dataset then either

use a DataView to do the filtering and copy it into another dataset

OR create your own class derived from XmlReader to do the filtering and pass
it to the overload of ReadXml. This reader should be quite simple as it just
has to filter the output of an XmlTextReader which will actually read the
file.
 

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

Back
Top