Parsing XML Data

Simple Man

Member
Joined
Jul 7, 2010
Messages
13
Programming Experience
5-10
Not sure if correct spot or not...

I have really never worked with XML files before. I've got a file that I need to extract data from...i guess a particular node. I've attached the file.

The data that I need is under meteohub/data --> timeframe="actual"
There are several items under this and I need to some how parse out the unit, cat, sensor, and of course the value.

for example:
<item unit="utc" cat="date" sensor="date0">20110413204001</item>


As you see in this file, there is another node "data" and the timeframe is "alltime"...and another called "day1" etc. I don't need these...only the actual.

How might I go about doing this?

Thanks.
 

Attachments

  • mydata.zip
    7.8 KB · Views: 24
Let me try and make this less confusing...

Here is a snippet of the xml data:
<?xml version="1.0"?>
<meteohub>
<config>
<language>en</language>
<temp_sensor print="°F" unit="f">th0</temp_sensor>
<hum_sensor print="%" unit="rel">th0</hum_sensor>
<dew_sensor print="°F" unit="f">th0</dew_sensor>
</config>

<data timeframe="actual">
<item unit="utc" cat="date" sensor="date0">20110413204001</item>
<item unit="utc" cat="date2" sensor="date0">13.04.2011 20:40:01</item>
<item unit="utc" cat="puredate" sensor="date0">13.04.2011</item>
<item unit="utc" cat="time" sensor="date0">20:40:01</item>
<item unit="utc" cat="year" sensor="date0">2011</item>
</data>

<data timeframe="24Hours">
<item unit="utc" cat="date" sensor="date0">20110413204001</item>
<item unit="utc" cat="date2" sensor="date0">13.04.2011 20:40:01</item>
<item unit="utc" cat="puredate" sensor="date0">13.04.2011</item>
<item unit="utc" cat="time" sensor="date0">20:40:01</item>
<item unit="utc" cat="year" sensor="date0">2011</item>
</data>
</meteohub>


Now, with this data, I only want to parse out data that is in node data that has an attribute of "actual". Within this node, I only want to retrieve data for elements that have an attribute cat = "date" and "date2" and attribute sensor = "date0" and "date0".

I don't want values from node data with attribute 24Hours nor Config.

I'd like to take these values and update a database.

Hope this is a little more helpful.

Thanks again.
 
Last edited:
LINQ to XML sample:
VB.NET:
Dim doc = XDocument.Load("mydata.xml")
Dim data = (From x In doc.<meteohub>.<data> Where x.@timeframe = "actual").Single
Dim items = From item In data.<item> _
            Where item.@sensor = "date0" And (item.@cat = "date" Or item.@cat = "date2")

For Each item In items
    Debug.WriteLine(String.Format("cat={0}, unit={1}, value={2}", item.@cat, item.@unit, item.Value))
Next
LINQ to XML was new in the version you're using, .Net 3.5, but there are also good Xml tools in System.Xml namespace that has always been there.
 
John,

The code you provided help me through what I wanted to do. I was able to take your example and tweak it. Thanks a lot!

So, the code you provided is LINQ? I'm not really new to vb.net, just haven't used it all that much. I'm still learning new things. I went to DevConnections a couple weeks back and I did see session on LINQ. I maybe should have went to a few sessions on this???

Thanks again.

Nick
 
Back
Top