Parsing an XML file from RSS feed with namespaces in C#

The task is to parse a XML file retrieved from the RSS feed.
Below is the sample partial XML file taken from a Leading Geographical alerts website’s RSS feeds.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/"> 
<channel> 
<pubDate>Fri, 4 Jan 2008 09:51 GMT+1</pubDate> 

<item> 

<title>Red alert: Tropical Cyclone SINLAKU-08.</title> 
<description>Tropical Cyclone SINLAKU-08 .</description> 
<pubDate>Tue, 16 Sep 2008 06:00 GMT+1</pubDate> 
<geo:point> 
<geo:lat>27</geo:lat> 
<geo:long>124</geo:long> 
</geo:point> 
</item> 
<item> 

<title>Red alert: Tropical Cyclone IKE-08 .</title> 
<description>Tropical Cyclone IKE-08 .</description> 
<pubDate>Sun, 14 Sep 2008 09:00 GMT+1</pubDate> 
<geo:point> 
<geo:lat>36.400001526</geo:lat> 
<geo:long>-92.5</geo:long> 
</geo:point> 
</item> 

</channel> 
</rss>

As we can we there are two items(alerts) in the file one for Tropical Cyclone SINLAKU-08 and other for Tropical Cyclone IKE-08.

Lets write the code in c# to extract title,latitude and longitude of both the alerts.

First of all, lets load the XML data into the XMLDocument instance.

//include namespace for working with xml files
using System.Xml;
public partial class GoogleMap : System.Web.UI.Page
{
//creating new instance of xml document
XmlDocument xmlDoc = new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
xmlDoc.Load(Server.MapPath("~/XML Files/sourceRssEDIS.xml"));
//rest of the code described later will also be added here
}
}

Now, introduce the namespaces used in the xml file to the xmlDoc with the XmlNamespaceManager class instance.

XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("geo", "http://www.w3.org/2003/01/geo/");

Select all the items in a array using GetElementsByTagName() method.

XmlNodeList nodes = xmlDoc.GetElementsByTagName("item");

Iterate for the elements inthe nodes array using for loop and select the latitude and longitude values using SelectNodes() method as shown below and then set the values of text of respective labels.

for (int i = 0; i < nodes.Count; i++)
{
XmlNodeList gpointnodeslat = nodes[i].SelectNodes("geo:point//geo:lat", nsMgr);
XmlNodeList gpointnodeslong = nodes[i].SelectNodes("geo:point//geo:long", nsMgr);
XmlNodeList titleNodes= nodes[i].SelectNodes("title");

Label lbllatitude=new Label();
Label lbllongitude = new Label();
Label lbltitle = new Label();
latitude.Text = gpointnodeslat[0].InnerText;
longitude.Text = gpointnodeslong[0].InnerText;
lbltitle.Text = titleNodes[0].InnerText;
}

In this way, any tag in the xml file can be searched for which is either with a namespace or not.

kick it on DotNetKicks.com
Shout it

DZoneDeliciousLinkedInRedditTechnorati FavoritesStumbleUponShare

3 comments

  1. Parsing an XML file from RSS feed with namespaces in C# | Tech Samosa…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

  2. Parsing an XML file from RSS feed with namespaces in C# | Tech Samosa…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  3. [...] in the file one for Tropical Cyclone SINLAKU-08 and other for Tropical Cyclone IKE-08. Lets… Read more… Categories: .NET  XML     Share | Related [...]

Leave a Reply

Your email address will not be published. Required fields are marked *