主要是用到了XmlTextReader,现在只是一个最基本的结构……很多东西以后添加完善。最后自己做一个RSS Reader~
使用方法:
Feed feed1=new Feed("rss.xml");
feed1.Parse();
for(int i=0;i<feed1.Count;i++)
Console.WriteLine(feed1[i].Title);
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;
namespace d_RSS_Reader
{
public class Feed
{
private string url;
private string title;
private string link;
private string pubDate;
private ArrayList items;
public bool IsParsed = false;
public bool IsLocalFeed = false;
public int Count;
public Feed()
{
items = new ArrayList();
}
public Feed(string url)
{
items=new ArrayList();
this.url = url;
}
public void Parse()
{
if (url == null)
{
throw new Exception("url is not defined");
}
FileStream feedfile = new FileStream(url, FileMode.Open,FileAccess.Read);
XmlTextReader reader = new XmlTextReader(feedfile);
reader.WhitespaceHandling = WhitespaceHandling.None;
reader.XmlResolver = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName == "channel")
{
ParseChannel(reader);
}
}
}
}
private void ParseChannel(XmlReader reader)
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
try
{
if (reader.LocalName == "item")
ParseItem(reader);
else if (reader.Depth == 2)
{
if (reader.LocalName == "title")
title = reader.ReadString();
if (reader.LocalName == "link")
link = reader.ReadString();
if (reader.LocalName == "pubDate")
pubDate = reader.ReadString();
}
}
catch (XmlException e)
{
}
}
if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName=="channel")
{
IsParsed = true;
Count = items.Count;
return;
}
}
}
private void ParseItem(XmlReader reader)
{
int startDepth = reader.Depth;
Item newItem = new Item();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.LocalName == "title")
newItem.Title = reader.ReadString();
if (reader.LocalName == "creator")
newItem.Creator = reader.ReadString();
if (reader.LocalName == "author")
newItem.Creator = reader.ReadString();
if (reader.LocalName == "pubDate")
newItem.PubDate = reader.ReadString();
if (reader.LocalName == "link")
newItem.Link = reader.ReadString();
if (reader.LocalName == "description")
newItem.Description= reader.ReadString();
}
if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "item")
break;
}
this.items.Add(newItem);
}
public void ParseURL(string url)
{
}
public Item this[int index]
{
get { return (Item)items[index]; }
}
public string URL
{
get { return url; }
set { url = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
public string Link
{
get { return link; }
set { link = value; }
}
public string PubDate
{
get { return pubDate; }
set { pubDate = value; }
}
}
public class Item
{
private string title;
private string creator;
private string pubDate;
private string link;
private string description;
public Item() { }
public Item(string title, string author, string pubDate, string link, string description)
{
this.title = title;
this.creator = author;
this.pubDate = pubDate;
this.link = link;
this.description = description;
}
public override string ToString()
{
return title;
}
public string Title
{
get { return title; }
set { title = value; }
}
public string Creator
{
get { return creator; }
set { creator = value; }
}
public string PubDate
{
get { return pubDate; }
set { pubDate = value; }
}
public string Link
{
get { return link; }
set { link = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
}
}