Read XML file, server returning error

2

I wrote a code to read RSS feed that works fine. If you access this URL you will see that it returns an XML in the browser, but when running in my code it generates an internal error in the server and I do not understand why. Can someone help me?

The code below is in dotNetFeedle . If you uncomment the second link and comment the first you can see the error.

using System;
using System.Xml;

public class Program
{
    public static void Main()
    {

    string urlXml = "http://g1.globo.com/dynamo/tecnologia/rss2.xml";
    //string urlXml = @"http://migalhas.com.br/rss/rss.xml";

    //Cria novo documento XML local
    XmlDocument doc = new XmlDocument();

    //Tenta Carregar XML remoto em nosso XML local
    try
    {
        doc.Load(urlXml);
        XmlNodeList rssItems = doc.SelectNodes("//item");   

        int i = 0;
        foreach (XmlNode node in rssItems)
        {
            i++;
            Console.WriteLine(i + " - " + node["title"].InnerText);
            if(i==5)
            break;
        }    
    }
    catch (Exception ex)
    {
        Console.WriteLine("Erro:" + ex.Message);
    }
    //Selcecionar nós desejados usando xPath

    }
}
    
asked by anonymous 06.12.2017 / 21:26

1 answer

2

Try this way with WebClient

//string urlXml = "http://g1.globo.com/dynamo/tecnologia/rss2.xml";
string urlXml = @"http://migalhas.com.br/rss/rss.xml";

var data = "";

using (var wc = new WebClient())
{
    wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
    data = wc.DownloadString(urlXml);                
}

var reader = new XmlTextReader(new System.IO.StringReader(data));
while (reader.Read())
{
    Console.WriteLine(reader.Value);
}
    
06.12.2017 / 23:15