M
M
Mikhail2018-02-14 23:27:49
C++ / C#
Mikhail, 2018-02-14 23:27:49

Is it OK to write an HTTP request to read RSS?

Hello. I'm writing an RSS reader and I have a couple of questions. The code is like this:

public static List<NewsRSS> ReadRSS(string Url)
        {
            List<NewsRSS> News = new List<NewsRSS>();
            try
            {
                WebRequest request = WebRequest.Create(Url);
                XmlTextReader NewsXmlReader = new XmlTextReader(request.GetResponse().GetResponseStream());
                XDocument NewsXml = XDocument.Load(NewsXmlReader);

                foreach (XElement NewsNode in NewsXml.Element("rss").Element("channel").Elements("item"))
                {
                   //тут просто выбираем нужные данные из XML
                }
            } catch (XmlException XmlErr)
            {
                Console.WriteLine("Ошибка при работе с XML: {0}", XmlErr.Message);
            } catch (HttpListenerException HttpErr )
            {
                Console.WriteLine("Ошибка при обработке запроса: {0}", HttpErr.Message);
            }

            return News;
        }

Actually, questions:
1. Is error handling normal? What confuses me is that if an error occurs, an empty news list will be returned.
2. Is the request sending logic written correctly? Maybe you need to somehow send requests asynchronously via async/await?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question