Search code examples
c#xml.net-micro-framework

XmlDocument Replacement


anyone know of another way of doing an XMLDocument in .NET micro framework? apparently, i am unable to create an XmlDocument object, and so i need to find a suitable replacement. Also, I cannot create an XmlNode object either.

What I am trying to do is grab information from an rss weather feed (XML) off the internet when a button is pressed.

//Grab wather data and display useful information.
System.Xml.XmlTextReader WeatherXML = new System.Xml.XmlTextReader(http://rss.theweathernetwork.com/weather/caab0211);
System.Xml.XmlDocument doc = new XmlDocument();
doc.Load(http://rss.theweathernetwork.com/weather/caab0211);
XmlNode node = doc.DocumentElement.SelectSingleNode("/rss/channel/item");
string attr = (node.LastChild.InnerText);

XML Document:

`<rss version="2.0">
    <channel>
        <title>The Weather Network - Medicine Hat, Alberta</title>
        <link>http://www.theweathernetwork.com/weather/caab0211?</link>
        <description>The Weather Network - ...</description>
        <copyright>copyright stuff</copyright>
        <language>en-us</language>
        <image>...</image>
        <image>...</image>
        <item>
            <title>Current Weather</title>
            <guid>http://www.theweathernetwork.com/weather/caab0211?ref=current_obs</guid>
            <link>http://www.theweathernetwork.com/weather/caab0211?ref=current_obs</link>
            <pubDate>Tue, 20 Jan 2015 11:45:00 -0500</pubDate>
            <description>Overcast, -1&nbsp;&deg;C   , Humidity  72% , Wind  W 18km/h</description>
        </item>`

I need that last description (Overcast, -1 °C , Humidity 72% , Wind W 18km/h)


Solution

  • You should be able to get the data from your XmlTextReader object, but you'll need to track where you are within the document.

    It looks like you're looking for the first channel/item/description element. You should be able to use a Stack to track your location as you Read through the reader.

    As a rough outline, push the LocalName onto the stack whenever the current NodeType is XmlNodeType.Element, and pop whenever it's EndElement. When it's an element, check whether stack.ToArray() matches {"description", "item", "channel"}. If it is, you've found the node you're looking for, so take the Value property of the reader.