Introduction
Ok, now that we've created our HttpWebRequest
/Response
program, we're going to extend that with XML (eXtensible Markup Language).
Why XML?
Because XML is becoming (if it isn't already) the (web)standard for exchanging data. As developers, we'd like to know how it works, isn't it? :)
Anyhow, this time we're going to create a little RSS reader (command line).
Using the Code
Ok, first, if you don't know about HttpWebRequest
/Response
yet, visit part 1.
So, we'll start off with some code:
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using System.IO;
namespace RSS_reader
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length < 1 || args.Length > 1)
{
Console.Write(string.Format
("Usage: {0} RSS-site{1}Example: {0}
http://www.codeproject.com/webservices/articlerss.aspx?cat=1",
"RSS-reader.exe",
Environment.NewLine));
return;
} else {
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(args[0]);
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
if (WebResp.StatusCode == HttpStatusCode.OK)
{
Stream Answer = WebResp.GetResponseStream();
XmlTextReader response = new XmlTextReader(Answer);
while (response.Read())
{
switch (response.NodeType)
{
case XmlNodeType.Element:
if (response.Name == "title" ||
response.Name == "description" ||
response.Name == "link" ||
response.Name == "author")
{
string t = response.Name;
response.Read();
Console.WriteLine(string.Format("{0}: {1}",
t, response.Value));
}
break;
}
}
response.Close();
} else
{
Console.Write(string.Format("Error, wrong statuscode: {0}",
WebResp.StatusCode.ToString()));
}
Console.Read();
}
}
}
}
Ok, I admit, it's VERY basic, but it kind of shows how it's easy to implement XML in your HTTP-oriented program. I'll be extending this article soon though.
History
- 31-03-2007 01:00 (local time - Belgium) Started writing document
- 31-03-2007 01:33 (local time - Belgium) Stopped writing document, this is v0.1