Now a days RSS feeds are getting popular. Their are many software available in market that reads rss feeds. Here i have wrote a simple rss reader fuction that returns a list of rss news of items.
public List ReadXML(String fileName)
{
List itemList = null;
try
{
XmlTextReader reader = new XmlTextReader(fileName);
reader.WhitespaceHandling = WhitespaceHandling.None;
itemList = new List();
RssItem objItem = null;
while (reader.Read())
{
bool isStart = reader.NodeType.Equals(XmlNodeType.Element);
bool isEnd = reader.NodeType.Equals(XmlNodeType.EndElement);
if (isEnd && reader.Name.Equals("item"))
objItem = null;
else if (isStart && reader.Name.Equals("item"))
{
objItem = new RssItem();
itemList.Add(objItem);
}
//read rss item tag and add into Hashtable
else if (isStart && objItem != null)
{
if (reader.Name.Equals("title"))
{
reader.Read();
objItem.Title = reader.Value;
}
else if (reader.Name.Equals("link"))
{
reader.Read();
objItem.Link = reader.Value;
}
else if (reader.Name.Equals("description"))
{
reader.Read();
objItem.Description = reader.Value;
}
}
}
}
catch (Exception e) { throw e; }
return itemList;
}
or we can use xml to linq also to parse xml,
public List ReadXML(String fileName)
{
// Loading from a file, you can also load from a stream
XDocument response = XDocument.Load(fileName);
IEnumerable results = from c in response.Descendants("item")
select new RssItem
{
title = (c.Element("title") == null) ? string.Empty : c.Element("title").Value,
link = (c.Element("link") == null) ? string.Empty : c.Element("link").Value,
description = (c.Element("description") == null) ? string.Empty : c.Element("description").Value
};
return results.ToList();
}
RssItem Class to store rss items
public class RssItem
{
public string Title {get ;set;}
public string Link {get ;set;}
public string Description {get ;set;}
}