Pages

A simple RSS parser

Various XML parsing technologies made the content delivery on web very flexible.I am experimenting with various rss parsing methods.One of the straight forward parsing technology is XML Pull Parsing.Xml Pull Parser (in short XPP) is a streaming pull XML parser and should be used when there is a need to process quickly and efficiently all input elements.The method is very flexible and there is no need for xml validation.I think this form is very useful for parsing simple xml documents.The parsing is designed by programmer itself requesting the parsing for each elements.He can omit elements and its subtree which are not needed.RSS are syndications through xml data.So a simple RSS parsing can be done by XPP method.The limitation that restricts XPP to a subset of XML documents is that it does not support entities, comments, or processing instructions in the document. XPP creates a document structure consisting only of elements, attributes (including Namespaces), and content text. This is a serious limitation for some types of applications.Anyway I try here to make a sample code.In this code an rss feed is read and create a string as a content inside div tag , so that the string can attach as innerhtml data for a dynamic page.I want to make it as a webservice, so that the content delivery can happen as a service.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace SampleWebService
{
public class Parser
{
StringBuilder sbString = new StringBuilder();
StringBuilder sbTempString = null;

public string GetXMLData()
{

XmlTextReader xtrReader = new XmlTextReader("http://rss.cnn.com/rss/cnn_world.rss");
xtrReader.WhitespaceHandling = WhitespaceHandling.None;
xtrReader.MoveToContent();


while (xtrReader.Read())
{
switch (xtrReader.NodeType)
{

case XmlNodeType.Element:
BuildElements(xtrReader);
break;

case XmlNodeType.EndElement:
BuildEndElements(xtrReader);
break;
}

}

return sbString.ToString();

}

private void BuildElements(XmlTextReader xtrReader)
{
switch (xtrReader.Name.ToLower())
{

case "item":
sbTempString = new StringBuilder();
sbTempString.Append("<div>");
break;

case "title":
if (sbTempString != null && sbTempString.Length > 0)
{
xtrReader.Read();
sbTempString.Append("<a>").Append(xtrReader.Value).Append( "</a><br />");
}
break;

case "link":
if (sbTempString != null && sbTempString.Length > 0)
{
xtrReader.Read();
sbTempString.Insert(sbTempString.ToString().LastIndexOf("<a") + 2, " href=\"" + xtrReader.Value +"\"");
}
break;

case "description":
if (sbTempString != null && sbTempString.Length > 0)
{
xtrReader.Read();
sbTempString.Append("<p>").Append(xtrReader.Value);
}
break;

}
}


private void BuildEndElements(XmlTextReader xtrReader)
{
switch (xtrReader.Name.ToLower())
{

case "item":
if (sbTempString != null && sbTempString.Length > 0)
{
sbTempString.Append("</div>");
sbString.Append(sbTempString);
}
break;

case "title":

break;

case "link":
break;

case "description":
if (sbTempString != null && sbTempString.Length > 0)
{
sbTempString.Append("</p>");
}
break;

}
}


}
}



No comments:

Post a Comment