C# – Easily consuming an XML or RSS feeds hosted on the web
August 14th, 2010 by JuanK
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;
}
- 1 Comentario »
- Publicado en la categoría 'C#'

Wordpress
Un comentario to “C# – Easily consuming an XML or RSS feeds hosted on the web ”
August 14th, 2010 at 10:41 pm
[...] C#- easily consuming an XML or RSS feed hosted on the web [...]