473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Deserializing XML feed (nested array problem)

Hiho,

I'm trying to deserialize the following XML feed from the weather
channel but I'm having an issue with the nested arrays. I got as far as
getting the 'day' into arrays but I can't get the 'part' nested array
working.

This is my first hack at XML feeds and such, so any pointers as to how
to best do this or references/tutorials would be good.
(I'm doing all this in .NET 2.0 btw)

Ok, here's a snip of the feed:

<weather>
<loc>...</loc>
<cc>...</cc>
<dayf>
<day d="1" t="Tuesday" dt="Jul 27">
<hi>65</hi>
<low>48</low>
<sunr>7:09 AM</sunr>
<suns>5:37 PM</suns>
<part p="d">
<icon>34</icon>
<t>Mostly Sunny</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>6</d>
<t>N</t>
</wind>
<ppcp>10</ppcp>
<hmid>63</hmid>
</part>
<part p="n">
<icon>45</icon>
<t>Showers Late</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>22</d>
<t>NNE</t>
</wind>
<ppcp>50</ppcp>
<hmid>86</hmid>
</part>
</day>
<day d="2" t="Wednesday" dt="Jul 28">
<hi>65</hi>
<low>48</low>
<sunr>7:08 AM</sunr>
<suns>5:38 PM</suns>
<part p="d">
<icon>30</icon>
<t>Partly Cloudy</t>
<wind>
<s>11</s>
<gust>N/A</gust>
<d>337</d>
<t>NNW</t>
</wind>
<ppcp>20</ppcp>
<hmid>71</hmid>
</part>
<part p="n">
<icon>29</icon>
<t>Partly Cloudy</t>
<wind>
<s>4</s>
<gust>N/A</gust>
<d>10</d>
<t>N</t>
</wind>
<ppcp>20</ppcp>
<hmid>87</hmid>
</part>
</day>
</dayf>
</weather>

And here's part of my class:

[XmlRoot("weather")]
public class Weather
{
public Loc loc;
public Cc cc;
[XmlArrayItem("day")]
public Day[] dayf;
}
*snip*

public class Day
{
[XmlAttribute]
public string t;

public string hi;
public string low;
}

Now, this is all I can get working. I know there should be a class
called Dayf as well but as soon as I try that, it stops working.
I wasn't able to find any examples as to how to do nested arrays in the
MS docs. Please bear with my noviceness in this issue :)

Cheers

Nov 12 '05 #1
1 2343
If I run your XML snippet through xsd.exe ,
I get an xsd (inferred).
I then tweaking that XSD to make the dayf element maxOccurs="1".
Then I run the resulting modified xsd through xsd.exe (xsd /c) and get a C#
source file that produces the classes you are seeking.
This code will then de-serialize and then re-serialize your original xml
snippet:

string path="weather.xml";
XmlSerializer s1 = new XmlSerializer(typeof(weather));
weather w = (weather) s1.Deserialize(System.IO.File.OpenText(path));
s1.Serialize(System.Console.Out,w);

-D

ps: this is using .NET Framework v1.1

"elixxir" <el*****@ucc.asn.au> wrote in message
news:cf********@odbk17.prod.google.com...
Hiho,

I'm trying to deserialize the following XML feed from the weather
channel but I'm having an issue with the nested arrays. I got as far as
getting the 'day' into arrays but I can't get the 'part' nested array
working.

This is my first hack at XML feeds and such, so any pointers as to how
to best do this or references/tutorials would be good.
(I'm doing all this in .NET 2.0 btw)

Ok, here's a snip of the feed:

<weather>
<loc>...</loc>
<cc>...</cc>
<dayf>
<day d="1" t="Tuesday" dt="Jul 27">
<hi>65</hi>
<low>48</low>
<sunr>7:09 AM</sunr>
<suns>5:37 PM</suns>
<part p="d">
<icon>34</icon>
<t>Mostly Sunny</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>6</d>
<t>N</t>
</wind>
<ppcp>10</ppcp>
<hmid>63</hmid>
</part>
<part p="n">
<icon>45</icon>
<t>Showers Late</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>22</d>
<t>NNE</t>
</wind>
<ppcp>50</ppcp>
<hmid>86</hmid>
</part>
</day>
<day d="2" t="Wednesday" dt="Jul 28">
<hi>65</hi>
<low>48</low>
<sunr>7:08 AM</sunr>
<suns>5:38 PM</suns>
<part p="d">
<icon>30</icon>
<t>Partly Cloudy</t>
<wind>
<s>11</s>
<gust>N/A</gust>
<d>337</d>
<t>NNW</t>
</wind>
<ppcp>20</ppcp>
<hmid>71</hmid>
</part>
<part p="n">
<icon>29</icon>
<t>Partly Cloudy</t>
<wind>
<s>4</s>
<gust>N/A</gust>
<d>10</d>
<t>N</t>
</wind>
<ppcp>20</ppcp>
<hmid>87</hmid>
</part>
</day>
</dayf>
</weather>

And here's part of my class:

[XmlRoot("weather")]
public class Weather
{
public Loc loc;
public Cc cc;
[XmlArrayItem("day")]
public Day[] dayf;
}
*snip*

public class Day
{
[XmlAttribute]
public string t;

public string hi;
public string low;
}

Now, this is all I can get working. I know there should be a class
called Dayf as well but as soon as I try that, it stops working.
I wasn't able to find any examples as to how to do nested arrays in the
MS docs. Please bear with my noviceness in this issue :)

Cheers

Nov 12 '05 #2

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

Similar topics

6
by: Martman | last post by:
I am new to PHP but testing the ability to read an RSS xml feed. I have successfully retrieved, read, and displayed an RSS feed from pcworld. However, when you click on one of the links it adds...
5
by: Artur Niesporek | last post by:
Hi guys, when receiving a soap response posted back from a server i get the following error when deserializing the soap message. The problem seems to be with the item-collection of the...
4
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
0
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the...
2
by: symbol | last post by:
I am having this problem in a managed c++ DLL which mixes managed and unmanaged C/C++ code. I tried to assign value to a struct array nested in another struct. but I can only write to the first...
0
by: jcsnippets.atspace.com | last post by:
Hi everyone, I'm trying to read an xml config file in C#. To do this, I have used the xsd.exe tool to create an xsd file from my xml, and to generate code to read this xml file. Here is a...
1
by: jcsnippets.atspace.com | last post by:
Hi everyone, I'm trying to read an xml config file in C#. To do this, I have used the xsd.exe tool to create an xsd file from my xml, and to generate code to read this xml file. Here is a...
1
by: Sjaakie | last post by:
Hi all, I used the xsd-util to turn the following XML-file into an .xsd and a class. <?xml version="1.0" encoding="utf-8" ?> <Settings> <ApplicationTitle>...</ApplicationTitle>...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.