How can I load data from an xml file into a DataTable with a condition?
i want to fill the data into datatable from xml file with certain condition?
You can't apply a condition when loading the data.
You can easily load an XML file into a DataTable, and then either
- create a DataView over the DataTable to show you only the rows you're interested in
- loop over the DataTable and delete out the unwanted rows
Marc
By far the simplest way to do this is to filter the data after reading it into a DataTable
. But that approach may not be ideal if, for instance, the XML file is extremely large and the set of filtered rows is small: you incur the time and space costs of allocating, processing, and destroying a very large number of objects in order to get a small number of them.
There's a way around this, but it's not trivial: subclass XmlReader
. Do the filtering in the subclass as it's reading the XML, returning only nodes that meet your filtering criteria to the caller of the Read()
method.
This MSDN article describes how to write an XmlReader
that allows (say) XmlDocument
to read .INI files as though they were XML documents. It's actually easier to write a filtering XmlReader
, as you don't have to deal with all of the parsing issues that are described in this article - you just have your XmlReader
subclass instantiate its own XmlReader
, and then return (or don't) nodes that it reads.