Connecting Tech Pros Worldwide Forums | Help | Site Map

XML to DataSet please help with selection

Arthur Dzhelali
Guest
 
Posts: n/a
#1: Nov 12 '05
I have a two xml files schema is identical.
When I read file into dataset and then bind dataset to the form.

These are weather files we are getting from weather service.

One file has only local weather so I don't have any problems with that,
but other has weather for several different locations.

Basically I need to select only weather for location if I know its code.
I should have exactly same dataset so I will be able bind it to the same
form without recoding.

I was trying to use dataview, but dataview works only with one table and
doesn't produce dataset.

I got it working by looping through table and removing rows that are not
correspond to the location I am interested in (rest of the related rows
removed automatically). This is works, but it is very inefficient and
dumb approach.
There is got to be a way to select from xml file only data I need so it
will create dataset only with one element I need.

Here is example of xml files.
If it is possible can someone give example using VB.net or C# so I don't
have create additional XSL pages


1************** This one works fine

<?xml version="1.0" encoding="ISO-8859-1" ?>
<weather>
<timestamp zone="GMT">02/11/2004 17:31</timestamp>
<current>
<citycode>LONX</citycode>
<day>
<name>Today</name>
<conditions wxcode="06">M/CLOUDY</conditions>
<temperature unit="F">38</temperature>
<apparent_temperature unit="F">38</apparent_temperature>
<wind_chill_temperature unit="F">29</wind_chill_temperature>
<humidity unit="%">35</humidity>
<pressure unit="INHG">29.99</pressure>
<windspeed unit="MPH">17</windspeed>
<wind_from>WNW</wind_from>
<visibility unit="MI">10</visibility>
</day>
</current>
<copyright>Copyright AccuWeather, Inc. 2004</copyright>
</weather>

2*************** This one needs to be fixed see comment

<?xml version="1.0" encoding="ISO-8859-1" ?>
<weather>
<timestamp zone="GMT">02/11/2004 17:31</timestamp>
<!-- cityCode will be passed to function and it should select current
only for this city -->
<current>
<citycode>OEMA</citycode>
<day>
<name>Today</name>
<conditions wxcode="35">P/CLOUDY</conditions>
<temperature unit="F">79</temperature>
<apparent_temperature unit="F">78</apparent_temperature>
<wind_chill_temperature unit="F">79</wind_chill_temperature>
<humidity unit="%">33</humidity>
<pressure unit="INHG">29.98</pressure>
<windspeed unit="MPH">10</windspeed>
<wind_from>W</wind_from>
<visibility unit="MI">7</visibility>
</day>
</current>
<current>
<citycode>OBBI</citycode>
<day>
<name>Today</name>
<conditions wxcode="33">CLEAR</conditions>
<temperature unit="F">66</temperature>
<apparent_temperature unit="F">68</apparent_temperature>
<wind_chill_temperature unit="F">66</wind_chill_temperature>
<humidity unit="%">88</humidity>
<pressure unit="INHG">30.07</pressure>
<windspeed unit="MPH">6</windspeed>
<wind_from>ENE</wind_from>
<visibility unit="MI">7</visibility>
</day>
</current>
<current>
<citycode>EHAM</citycode>
<day>
<name>Today</name>
<conditions wxcode="07">CLOUDY</conditions>
<temperature unit="F">41</temperature>
<apparent_temperature unit="F">44</apparent_temperature>
<wind_chill_temperature unit="F">36</wind_chill_temperature>
<humidity unit="%">90</humidity>
<pressure unit="INHG">N/A</pressure>
<windspeed unit="MPH">6</windspeed>
<wind_from>NNE</wind_from>
<visibility unit="MI">8</visibility>
</day>
</current>
<current>
<citycode>EANX</citycode>
<day>
<name>Today</name>
<conditions wxcode="38">M/CLOUDY</conditions>
<temperature unit="F">46</temperature>
<apparent_temperature unit="F">49</apparent_temperature>
<wind_chill_temperature unit="F">44</wind_chill_temperature>
<humidity unit="%">84</humidity>
<pressure unit="INHG">N/A</pressure>
<windspeed unit="MPH">4</windspeed>
<wind_from>NNW</wind_from>
<visibility unit="MI">7</visibility>
</day>
</current>
<copyright>Copyright AccuWeather, Inc. 2004</copyright>
</weather>

Derek Harmon
Guest
 
Posts: n/a
#2: Nov 12 '05

re: XML to DataSet please help with selection


"Arthur Dzhelali" <a.dzhelali@theday.com> wrote in message news:Xns948C87D5CE1BCadzhelalithedaycom@216.168.3. 44...[color=blue]
> Basically I need to select only weather for location if I know its code.[/color]
: :[color=blue]
> There is got to be a way to select from xml file only data I need so it
> will create dataset only with one element I need.[/color]

Given the weather XML has been loaded into an XmlDocument,
one approach is to:

1. Use an XPath query to retrieve the timestamp and current
element of the city you're interested in.
2. Create a new XmlDocument whose weather element contains
only the two children: timestamp and the city of interest.

Then create the data set from the filtered document you possess
at the end of step 2. Here is a C# snippet that demonstrates this
procedure:

- - - GetSingleCity.cs (excerpt)
using System;
using System.Xml;
// . . .
public XmlDocument GetSingleCity( XmlDocument src, string cityCode)
{
XmlNode current, tstamp;
XmlDocument resultDoc = null;

current = srcDoc.SelectSingleNode(
String.Format(
"/weather/current[./citycode='{0}']",
cityCode.ToUpper( ) ) );

if ( null != current )
{
resultDoc = new XmlDocument( );

XmlElement weather = resultDoc.CreateElement( "weather");
XmlNode tstamp = src.SelectSingleNode( "/weather/timestamp");

if ( null != tstamp )
{
weather.AppendChild( resultDoc.ImportNode( tstamp, true));
}

weather.AppendChild( resultDoc.ImportNode( current, true));
resultDoc.AppendChild( weather);
}
return resultDoc;
}
// . . .
- - -

The key in the above code was the XPath expression,

/weather/current[./citycode='{0}']

which selects a current element with the provision that it only takes
the current element having a child element named citycode matching
an argument string (the all-caps version of the cityCode parameter
in the above code snippet). The portion of the expression inside of
the square brackets is called a "predicate," and can be used to filter
the resulting node set of the original path expression, /weather/current.

The code in the if-block uses XML DOM programming to construct
an XmlDocument, and then creates its root weather element. To this
weather element, a copy of the source document's timestamp is
added as the first child. A copy of the current element (located by
the XPath query above) is then added as the second child.

Notice here I say "copy of," because as you observe I use the
ImportNode( ) method to do a deep clone of these XML DOM
nodes. This is a necessary step to establish the nodes within
the result document. The only nodes that can be added to an
XmlDocument are those that (a) are created by that document,
or (b) are imported (through cloning) into that document. This
is because the node can only be owned by one document.

Finally, the weather element (and its two children) are added to
the XmlDocument before returning the filtered document. The
method returns null when the cityCode does not exist in the
weather information represented within the source document.


Derek Harmon


Arthur Dzhelali
Guest
 
Posts: n/a
#3: Nov 12 '05

re: XML to DataSet please help with selection


Thanks a lot.
Closed Thread


Similar .NET Framework bytes