473,832 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("weathe r")]
public class Weather
{
public Loc loc;
public Cc cc;
[XmlArrayItem("d ay")]
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 2370
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.x ml";
XmlSerializer s1 = new XmlSerializer(t ypeof(weather)) ;
weather w = (weather) s1.Deserialize( System.IO.File. OpenText(path)) ;
s1.Serialize(Sy stem.Console.Ou t,w);

-D

ps: this is using .NET Framework v1.1

"elixxir" <el*****@ucc.as n.au> wrote in message
news:cf******** @odbk17.prod.go ogle.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("weathe r")]
public class Weather
{
public Loc loc;
public Cc cc;
[XmlArrayItem("d ay")]
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
2324
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 http://localhost to the link located in the xml file. If I view source in the browser everything appears okay. Does anyone know of a configuration in the apache or php config files that automatically adds http://localhost to hyperlinks? If not,...
5
4635
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 nightlyrate-Array. .] Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Re ad6_nightlyratearray()
4
7516
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 the data but I can't figure out where the data is in the resulting array. Most of the relevant code is below. When I run the code to desrialize I get no errors but if I try to look at some of the data via the command window I get errors such as...
0
1992
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 objects contain other objects so when I declare my XmlSerializer I need to send along an array of types to be included in the serialization. That works fine until I run into 2 classes with the same name from different asseblies. This causes an XML...
2
1702
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 element in array, not the others. struct struct2{ int a; int b; };
0
1004
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 small sample of my xml file: <feed> <location> <directory>data</directory>
1
2339
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 small sample of my xml file: <feed> <location> <directory>data</directory>
1
1581
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> <DebugEmail>...</DebugEmail> <Proxy> <Enabled>...</Enabled>
1
11820
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 XmlNodeList. I am trying to deserialize the nodes using a foreach as follows: foreach(XmlNode lineItem in LineItemsNodeList) An abbreviated example of the nested lineItem node looks like this:
0
9794
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
9642
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
10780
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
10497
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
10539
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
10212
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
9319
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...
1
4420
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.