By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Deserializing XML feed (nested array problem)

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.