How to do a Select on a DataSet that was read from an Xml file? searching on an element attribute as

  • Thread starter ~~~ .NET Ed ~~~
  • Start date
N

~~~ .NET Ed ~~~

Hi,
I am reading the attached XML file into a DataSet and that is so far OK.
The problem is that I need to select a tree by its attribute (the Country
node tree whose code attribute is XYZ) so that the selected child nodes (of
the selected tree) are then bound to a DropDownList. I am afraid I haven't
done that so I do not know how to do that.
I think the code below simply gets all Country nodes, but I need to select
only the one with a matching country code

Sample code:
DataSet dsProvinces = new DataSet("Country");
dsProvinces.ReadXmlSchema(schemaFile); // schema was generated in
VS.NET from XML file
dsProvinces.ReadXml(file);
this.DropDownListProvince.DataSource = dsProvinces;
this.DropDownListProvince.DataMember = "Country";

Sample XML file:
<?xml version="1.0" encoding="utf-8"?>

<ProvinceList xmlns="http://localhost/CyberRealty/backend/Provinces.xsd">

<Country code="BE" name="Belgium">

..... some other nodes

</Country>

<Country code="NL" name="Netherlands">

<Province id="DR">Drenthe</Province>

<Province id="FV">Flevoland</Province>

<Province id="FR">Friesland</Province>

<Province id="GE">Gelderland</Province>

<Province id="GR">Groningen</Province>

<Province id="LI">Limburg</Province>

<Province id="NB">Noord Brabant</Province>

<Province id="NH">Noord Holland</Province>

<Province id="OI">Overijssel</Province>

<Province id="UT">Utrecht</Province>

<Province id="ZE">Zeeland</Province>

<Province id="ZH">Zuid Holland</Province>

</Country>

</ProvinceList>
 
A

Anubhav Mishra

Create a dataview with the filter that you require and bind the dataview to
the dropdown
Anubhav
 
C

Cor Ligthert

Ed,

Two questions, this should be a webform solution because there is no
dropdownlist control in winform, however after a webform there would be a
databind.

The acting of a webform looks the same as a winform, when you starting with
it, being busy you notice that it only looks.

Second what do you want to show, the countries or the provincies?

I have puzzled out that it because you show those can be the provincies from
the selected country, from your text it would be the countries, but that
does not fit with the selected text in your problem.

So can you tell us what you want?

Cor
 
N

~~~ .NET Ed ~~~

Hi Cor,
Yes you are right, it is a web application. The drop down list is
supposed to show the provinces of the selected country. The country itself
is already selected in another dropdown which then attempts to re-populate
the province (that is what I am missing) drop down on its SelectionChanged
event.

I am afraid I am not yet familiar with dataviews of ADO.NET so that doesn't
tell me much except it is possible.


Em.
 
C

Cor Ligthert

Ed,

Here a simple sample of that dataview and that rowfilter

\\\
Dim dv As New DataView(TheDataSet.Tables("TheProvinceTable")
dv.RowFilter = "Country = 'Nederland'" 'rom your country table
DropDownList1.DataSource = dv
DropDownList1.DataTextField = "Province"
DropDownList1.DataBind()
///

I hope this helps?

Cor
 
N

~~~ .NET Ed ~~~

Hi Cor,
That doesn't work (already tried it on the debugger). What happens is
that I should select on the country code which is the two-letter value of
the "code" attribute of the Country element. So I have something like:

private void PopulateProvinces(string countryCode)
{
DataSet dsCountries = new DataSet("ProvinceList");
dsCountries.ReadXmlSchema("Provinces.xsd"); // Schema was
generated with VS.NET from the xml file
dsCountries.ReadXml("Provinces.xml"); // Read in the
XML file of the original post

DataView dvCountries = new DataView(dsCountries.Tables["Country"]);
dvCountries.RowFilter = string.Format("code = '{0}'", countryCode);
// <Country code="NL" name="...">
dvCountries.Sort = "code";

this.DropDownListProvince.DataSource = dvCountries;
this.DropDownListProvince.DataTextField = "Province";
this.DropDownListProvince.DataBind();
}

No exception is produced but in debug mode I can watch the data set and it
shows two tables (Countries, Province) and the Countries table shows a count
of zero (0)....

Additionally, I consider "Province" to be the "records" of the "Countries"
table, so if I were using a database I would have a Provinces table with the
following columns:
CountryCode (NL,BE,etc.) -- The 'code' attribute of my Countries
element
ProvinceName (Zuid Holland) -- The inner text of each Province
element of the Country node
ProvinceID (ZH) -- The 'id' attribute of the
Province element

Am I doing anything wrong here? is there an easier way other than just
plunging into a real database?
 
C

Cor Ligthert

Ed,

My sample is a tried sample?

The problem for me is that you have attributes in your XML file, which are
not direct in a dataset.

A dataset is a collection of tables, which have a collection of rows which
have a collection of items where the last are described by the datacolumns.
A dataset holds also a collection of relations.

However my expirience is that you can read it in using that XSD and than you
get a dataset.

I have assumed that there are two tables build in the dataset. And you are
now using one.
When you can first write your dataset to disk you probably get a better view
on your dataset, when you look to it than in the notebook.

dsCountries.WriteXML("diskpath").

I hope this goes

Cor




Cor
 
N

~~~ .NET Ed ~~~

Hi Cor,
Thanks, I did try what you said except I did it in C#. I did not work and
I think the problem lies in the fact that I am using (for convenience) an
attribute in the parent element rather than having to replicate the same
attribute in all the child (Province) elements. All the examples I have seen
on the web are kind of textbook examples, that is simple scenarios.

As for the information on the XML file, it is not generated on the fly so
there is no DataSet writing it. I think I will just forget about this for
now and instead read the Xml file, search it using an XPath and then getting
the Xml child nodes of the result to populate the drop down list.

Thanks anyway,
 
C

Cor Ligthert

Ed,

I could not know you are using C# (otherwise I had done it in C#, when I
don't know it is VBNet).

Your conclussion is correct and was as I had in the beginning always as
well.

However that XSD will eat a lot, so when you mail me your XML file, than I
can have a second look for it. From your sample in the message I could not
make anything in a generic way and that is what it should be of course.

Cor
 

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