473,666 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML to DataSet please help with selection

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>LON X</citycode>
<day>
<name>Today</name>
<conditions wxcode="06">M/CLOUDY</conditions>
<temperature unit="F">38</temperature>
<apparent_tempe rature unit="F">38</apparent_temper ature>
<wind_chill_tem perature unit="F">29</wind_chill_temp erature>
<humidity unit="%">35</humidity>
<pressure unit="INHG">29. 99</pressure>
<windspeed unit="MPH">17</windspeed>
<wind_from>WN W</wind_from>
<visibility unit="MI">10</visibility>
</day>
</current>
<copyright>Copy right 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>OEM A</citycode>
<day>
<name>Today</name>
<conditions wxcode="35">P/CLOUDY</conditions>
<temperature unit="F">79</temperature>
<apparent_tempe rature unit="F">78</apparent_temper ature>
<wind_chill_tem perature unit="F">79</wind_chill_temp erature>
<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>OBB I</citycode>
<day>
<name>Today</name>
<conditions wxcode="33">CLE AR</conditions>
<temperature unit="F">66</temperature>
<apparent_tempe rature unit="F">68</apparent_temper ature>
<wind_chill_tem perature unit="F">66</wind_chill_temp erature>
<humidity unit="%">88</humidity>
<pressure unit="INHG">30. 07</pressure>
<windspeed unit="MPH">6</windspeed>
<wind_from>EN E</wind_from>
<visibility unit="MI">7</visibility>
</day>
</current>
<current>
<citycode>EHA M</citycode>
<day>
<name>Today</name>
<conditions wxcode="07">CLO UDY</conditions>
<temperature unit="F">41</temperature>
<apparent_tempe rature unit="F">44</apparent_temper ature>
<wind_chill_tem perature unit="F">36</wind_chill_temp erature>
<humidity unit="%">90</humidity>
<pressure unit="INHG">N/A</pressure>
<windspeed unit="MPH">6</windspeed>
<wind_from>NN E</wind_from>
<visibility unit="MI">8</visibility>
</day>
</current>
<current>
<citycode>EAN X</citycode>
<day>
<name>Today</name>
<conditions wxcode="38">M/CLOUDY</conditions>
<temperature unit="F">46</temperature>
<apparent_tempe rature unit="F">49</apparent_temper ature>
<wind_chill_tem perature unit="F">44</wind_chill_temp erature>
<humidity unit="%">84</humidity>
<pressure unit="INHG">N/A</pressure>
<windspeed unit="MPH">4</windspeed>
<wind_from>NN W</wind_from>
<visibility unit="MI">7</visibility>
</day>
</current>
<copyright>Copy right AccuWeather, Inc. 2004</copyright>
</weather>
Nov 12 '05 #1
2 1775
"Arthur Dzhelali" <a.********@the day.com> wrote in message news:Xn******** *************** *********@216.1 68.3.44...
Basically I need to select only weather for location if I know its code. : : 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.


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.c s (excerpt)
using System;
using System.Xml;
// . . .
public XmlDocument GetSingleCity( XmlDocument src, string cityCode)
{
XmlNode current, tstamp;
XmlDocument resultDoc = null;

current = srcDoc.SelectSi ngleNode(
String.Format(
"/weather/current[./citycode='{0}']",
cityCode.ToUppe r( ) ) );

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

XmlElement weather = resultDoc.Creat eElement( "weather");
XmlNode tstamp = src.SelectSingl eNode( "/weather/timestamp");

if ( null != tstamp )
{
weather.AppendC hild( resultDoc.Impor tNode( tstamp, true));
}

weather.AppendC hild( resultDoc.Impor tNode( current, true));
resultDoc.Appen dChild( 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
Nov 12 '05 #2
Thanks a lot.
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
403
by: Vaap | last post by:
I am trying to get dataset working through IDbDataAdapter interface as my solution need to work with Sql Server and Oracle ODP. I am using different data provider factories and the code here only shows SQL Server. My code snippets are // Build the interface in provider factory (_pf in code below) public IDbDataAdapter CreateDataAdapter() {
7
1801
by: Marty | last post by:
Hi, Ok I use the OLEDBConnector and dataset to retrieve data from my Access DB. I have a problem to read/parse the dataset and I would like to know if I am using the right object to reach my goal. And I don't want to spend hours to find out, would you tell me if I'm going in the wrong path? Private dbConnector As New OleDbConnection
0
1057
by: TonyM | last post by:
Hi All, Please bear with me on this, I'm not 100% sure I know how to ask this clearly. I have a Web Application that I'm trying to keep the items you can select in a DropDownList in a collection. -- This works fine, until I want to update that value with a field that's in a dataset that is populated from a SQL database.
3
3423
by: Michelle Stone | last post by:
Hi all I recently changed the schema of a dataset (added one field, and changed another field type from STRING to DATE). I want this reflected in my Crystal Reports Designer. Please don't tell me to recreate the report as a lot of work has already been spent on this report.
3
3050
by: Michelle Stone | last post by:
Thanks for your message I tried again and again... but didn't work. Can u please detail the exact steps to perform? This is what I do. In field explorer, i right-click the dataset to update. Then I click on SET LOCATION. When the list comes up, I already have the same dataset as the "CURRENT DATASET" and I have the same dataset in the tree control on the right as the default selection. I just click on REPLACE and close the dialog. But...
2
6323
by: Fraggle_Rock_1 | last post by:
I have a dataset like so |ID | type | Name ------------------ |1 | 1 | ham |2 | 2 | fish |1 | 2 | lard |2 | 1 | spam
2
1743
by: Danny Ni | last post by:
Hi, I would like to know the fastest way to clone a dataset with filter inVB.Net or C#. Say I have a dataset that has one datatable having several data rows. I want to clone the structure to another dataset but with selection of datarows. Please Help!
5
1533
by: mydogisbox | last post by:
I have two datasets. One dataset table from dataset1 is bound to a list box. On selection in the list box dataset2 has 9 tables that are populated from the database. these tables are then data-bound to 9 combo boxes. Following selection of items in the combo boxes and the click of a button, the results are then manually written back to the first table in dataset1. The problem that I am having is that after the selection in the first combo...
5
3377
by: Franck | last post by:
Ok here's my question. I have a program which can run both online or offline. Online work on a Webservice getting data from SQL2000. When the person want go offline he have to be online first and few tables are being saved locally in XML (non sensitive data of course). In offline mode the program is quite limited in things you can do. In the online mode(full mode) there is alot of very complex T-SQL. And these have to be repeated using...
0
8443
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.