Main Site Documentation

Cobra Networking/ XmlReader Help Needed


#1

I’m newly back into programming after a 25 year diversion, and I’m brand-new to embedded processors; Yikes! I’m using a Fez Cobra and trying to use XmlReader to read from an RSS website. I suspect that my problem has more to do with the network setup than the XmlReader. My sample code including output is below. Any help that you can give me would be greatly appriciated. Thank you, Carl

using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Xml;
using System.IO;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;

namespace FEZ_Cobra_Console_Application1
{
    public class Program
    {
        public static void Main()
        {
            // PROGRAM INTENDED TO RUN ON FEZ COBRA

             if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Length < 1)
            {
                Debug.Print("No Active network interfaces. Bombing out.");
                Thread.CurrentThread.Abort();
            }
 
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
 
            if (NI.IsDhcpEnabled == false)
            {
                Debug.Print("Enabling DHCP.");
                NI.EnableDhcp();
                Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            }
            else
            {
                Debug.Print("Renewing DHCP lease.");
                NI.RenewDhcpLease();
                Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            }
             
            XmlReader xmlDoc;

            try
            {
                WebRequest aRequest = WebRequest.Create("http://rss.cnn.com/rss/cnn_us.rssurlStr");
                WebResponse aResponse = aRequest.GetResponse();

                xmlDoc = XmlReader.Create(aResponse.GetResponseStream());

                Debug.Print("About to do XML Read");
                do
                {
                    xmlDoc.Read();
                    Debug.Print(xmlDoc.Name);
                }
                while (!xmlDoc.EOF);
                Debug.Print("XML Read completed");
 
                xmlDoc.Close();
            }
            catch
            {
                Debug.Print("Error doing Xml Read");
            }
        }

    }
}

FEZ COBRA OUTPUT IS AS FOLLOWS:

Renewing DHCP lease.
DCHP - IP Address = 10.0.0.25 ... Net Mask = 255.0.0.0 ... Gateway = 10.0.0.60
About to do XML Read
#### Exception System.Xml.XmlException - 0xdd000000 (1) ####
#### Message: 
#### System.Xml.XmlReader::Read [IP: 00d1] ####
#### FEZ_Cobra_Console_Application1.Program::Main [IP: 00e8] ####
A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Error doing Xml Read
The thread '<No Name>' (0x1) has exited with code 0 (0x0).
Done.

#2

CarlJ,

I don’t think there’s anything wrong with the networking setup. I believe this is probably an issue with the encoding of the XML being returned. If you examine the stream coming back from that site, you’ll see this at the start:

<?xml version="1.0" encoding="ISO-8859-1"?>

I believe the problem is that the encoding is defined as “ISO-8859-1”, which causes the XML parser to choke. See this post http://www.tinyclr.com/forum/2/1008/ for more info. You might have to manually remove that node or manually change the encoding value to “utf-8” in order for it to work. Hope that helps.


#3

CarlJ,

please use the code button when you include code, makes it much easier to read.

Eric


#4

check out the free beginners ebook at the downloads page.
It has an example of reading xml (page 130)


#5

Foekie you should make an XML helper people can use in their projects :slight_smile:

Secondly, put it on Fezzer!


#6

Sure, Josh. I have been working with SD though.
Let me mod it a bit for you, then I will upload it :slight_smile:


#7

Thanks guys for helping me understand the Xml Reader problem that I am having. However, I’d still rather read directly from my web steam if possible. I’m wondering if there is a way to read/ possition past this offending "encoding=“ISO-8859-1” in the web stream?
By the way, what/ where is Fezzer? Thanks again, Carl


#8

Where: http://www.fezzer.com

What: Sharing code for .Net Micro Framework was never so Freakin’ Easy!


#9

Carl, I believe reading from a web stream and reading from a sd does not differ that much actually. The only difference is persistent storage and web stream.

Give me some time and I will show you my example :wink: