tags:

views:

110

answers:

4

How can I load data from an xml file into a DataTable with a condition?

A: 

XPath allows you to do that, but please provide more details.

JG
want to fill the data into datatable from xml file with certain condition?
Amit Dhall
But what is the condition, and what does your XML file look like ? Please edit your original post with that information.
JG
A: 

i want to fill the data into datatable from xml file with certain condition?

Amit Dhall
The answers are... to make answers! Not to repeat the same question.
PhiLho
+1  A: 

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

marc_s
ok thanks a lot
Amit Dhall
A: 

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.

Robert Rossney