Microsoft MVP

Email y Rss

email rss

Klout

Seguidores en facebook

Timeline de mi Twitter

Tienes preguntas?

Ideas de un Conejo
Más allá de los sistemas de información: (C#)=> videojuegos, soluciones a problemas interesantes y Sistemas Operativos
XNA
C#
Eventos
Sistemas Operativos
Review
Varios
PL/SQL
Acerca de

C# – Easily consuming an XML or RSS feeds hosted on the web

August 14th, 2010 by JuanK

TweetFollow @JuanKRuiz

Share

Many times we need to read an RSS or XML content from the web, and we need it fast, light and easy to use.

Searching the Internet, you can get several solutions in several different flavors. In this article I’d teach how to build a component capable of read any XML that is on the web in order to access your information easily and by the way to explain What to do to take full advantage when that XML is a RSS .

We can split the problem in 2 parts:

  • Get the XML or RSS Web
  • Access to such information by a well-known component, a DataSet

 

GET THE RSS OR XML FROM THE WEB

To achieve this objective it’s necessary to make use of an HttpWebRequest object to create a request to the URL where the XML is hosted and then to capture the answer (response).

public static DataSet GetXMLDataSet(string URL)
{
    HttpWebRequest xmlRequest = (HttpWebRequest)WebRequest.Create(URL);
    WebResponse xmlResponse = xmlRequest.GetResponse();
}

 

ACCESS TO THIS INFORMATION THROUGH A KNOWN, A DATASET COMPONENT

Although we already have the WebResponse this cannot allow us for ‘multi-purpose’ access to the information it contains, but because we know that our WebResponse contains XML information, We can take it to create a DataSet.

You can create the DataSet of a Stream so we will use the GetResponseStream method to obtain it, we created a void DataSet and then we fill it with ReadXml.

public static DataSet GetXMLDataSet2(string URL)
{
    HttpWebRequest xmlRequest = (HttpWebRequest)WebRequest.Create(URL);
    WebResponse xmlResponse = xmlRequest.GetResponse();
    Stream responseStream = xmlResponse.GetResponseStream();

    DataSet xmlData = new DataSet();
    xmlData.ReadXml(responseStream);
    return xmlData;
}

By reducing code a little:

public static DataSet GetXMLDataSet(string URL)
{
    HttpWebRequest xmlRequest = (HttpWebRequest)WebRequest.Create(URL);
    DataSet xmlData = new DataSet();
    xmlData.ReadXml(xmlRequest.GetResponse().GetResponseStream());
    return xmlData;
}

That’s all, now is time to take advantage of the DataSet .

 

ADDITIONAL NOTE

Some RSS have a little more complex structures that can bring problems to create the DataSet, usually this problem throws System.Data.DuplicateNameException exception, to avoid this problem, it is necessary to tell the DataSet which is the correct structure of the RSS, so we use ReadXmlSchema loading the XSD schema that describes the structure of RSS 2.0, to obtain it just go to this page and download it: RSS 2.0 Schema, more specifically this link rss-2_0.xsd.

We add the xsd file to the project embedded resource, the code look like this:

/// <summary>
/// Se conecta a una URL que representa un archivo XML y convierte la información en un DataSet</summary>
/// <param name="URL">URL del xml publicado en la web</param>
/// <returns>Dataset que representa los datos XML</returns>
public static DataSet GetXMLDataSet(string URL)
{
    HttpWebRequest xmlRequest = (HttpWebRequest)WebRequest.Create(URL);
    DataSet xmlData = new DataSet();
    xmlData.ReadXmlSchema(new XmlTextReader(Resources.rss_2_0, XmlNodeType.Document,null));
    xmlData.ReadXml(xmlRequest.GetResponse().GetResponseStream(), XmlReadMode.IgnoreSchema);
    return xmlData;
}
Print Friendly
Share

TweetFollow @JuanKRuiz

  • 1 Comentario »
  • Publicado en la categoría 'C#'

Un comentario to “C# – Easily consuming an XML or RSS feeds hosted on the web ”


  • C# - 10 steps to consume "The Washington Post" Top news RSS in an IE Toolbar | Ideas de un Conejo Says:
    August 14th, 2010 at 10:41 pm  

    [...] C#- easily consuming an XML or RSS feed hosted on the web [...]

Deja un comentario

Redes Sociales

Follow @JuanKRuiz
Answer Questions

Busca en el blog

Artículos Relacionados

  • C# – Consumir un XML o un RSS alojado en la web de manera sencilla
  • C# – 10 pasos para Consumir el RSS de Top news de “The Washington Post” en un Toolbar para IE
  • C# – 10 steps to consume "The Washington Post" Top news RSS in an IE Toolbar
  • Artículos Relacionados

  • C# – Consumir un XML o un RSS alojado en la web de manera sencilla
  • C# – 10 pasos para Consumir el RSS de Top news de “The Washington Post” en un Toolbar para IE
  • C# – 10 steps to consume "The Washington Post" Top news RSS in an IE Toolbar
  • Nube de Temas

    API - C# - codigo - Fiber - Forms - GeSHi - icon - icono - IE - IE9 - imagenes - IT - operativo - Pinned - PowerShell - Proceso - rendimiento - RSS - sistema - Sistemas Operativos - Site - stack - Thread - velocidad - Visual - WCF - Windows - WndProc - WPF - XNA

    Blogs recomendados

  • VBCodigoPocketPC Espacio para tratar temas de programacion para dispositivos moviles, Pocket PC, Compact Framework, Embbeded Visual Basic, Visual Basic.NET , C# (C Sharp)
  • Róbinson Moscoso Estaré publicando acá cosas sobre tecnologia .NET, situacioines cotidianas de las que voy aprendiendo… sirve como extensión de memoria.
  • .Net C# Blog de Nelsón Venegas
  • Warnov Microsoft Developer Evangelist
  • IT LIfe Blog de mi Hermano que esta en el lado claro: IT
  • Sorey Garcia Una chica del común con la firme intención de no serlo
  • Black Byte videojuegos, modelado y animación 3d
  • Road to IT World Cosas interesantes de IT
  • Marcela Chitiva Un poco de esto… un poco de aquello
  • Surviving the Nigth El mejor blog para aquellos que nos gustan los “internals”
  • Meta

    1. Log in
    2. WordPress

    Ideas de un Conejo is powered by Wordpress. Theme designed by Juan Carlos Ruiz.