发布一个RSS辅助类
http://www.cnblogs.com/surfsky/archive/2009/12/03/1616230.html
发布一个RSS辅助类,是使用 XmlSerializer 解析和编码rss xml的。希望对大家有用:)
参考文档:
·http://cyber.law.harvard.edu/rss/rss.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Kingsoc;
namespace Kingsoc.Net
{
/// <summary>
/// RSS 单元
/// </summary>
public class RssItem
{
[XmlElement("title")] public string Title;
[XmlElement("link")] public string Link;
[XmlElement("description")] public string Description;
[XmlElement("author")] public string Author;
[XmlElement("category")] public string Category;
[XmlElement("comments")] public string Comments;
[XmlElement("enclosure")] public string Enclosure;
[XmlElement("guid")] public string Guid;
[XmlElement("pubDate")] public string PubDate;
[XmlElement("source")] public string Source;
}
/// <summary>
/// RSS 频道
/// </summary>
[XmlInclude(typeof(RssItem))]
public class RssChannel
{
[XmlElement("title")] public string Title;
[XmlElement("link")] public string Link;
[XmlElement("description")] public string Description;
[XmlElement("language")] public string Language;
[XmlElement("copyright")] public string CopyRight;
[XmlElement("managingEditor")] public string ManagingEditor;
[XmlElement("webMaster")] public string WebMaster;
[XmlElement("pubDate")] public string PubDate;
[XmlElement("lastBuildDate")] public string LastBuildDate;
[XmlElement("category")] public string Category;
[XmlElement("docs")] public string Docs;
[XmlElement("cloud")] public string Cloud;
[XmlElement("ttl")] public string TTL;
[XmlElement("image")] public string Image;
[XmlElement("rating")] public string Rating;
[XmlElement("textInput")] public string TextInput;
[XmlElement("skipHours")] public string SkipHours;
[XmlElement("skipDays")] public string SkipDays;
[XmlElement("item")]
public List<RssItem> Items = new List<RssItem>();
}
/// <summary>
/// RSS
/// </summary>
[XmlInclude(typeof(RssChannel))]
[XmlRoot("rss")]
public class Rss
{
[XmlElement("channel")]
public RssChannel Channel;
// 从XML字符串中解析
public static Rss LoadXmlString(string xml)
{
return SerializationHelper.LoadXmlString(xml, typeof(Rss)) as Rss;
}
// 保存为XML字符串
public string SaveXmlString()
{
return SerializationHelper.SaveXmlString(this);
}
// 读取指定URL的RSS,并解析之
public static Rss GetRss(string url)
{
string xml = FetchPage(url);
return SerializationHelper.LoadXmlString(xml, typeof(Rss)) as Rss;
}
// 创建示例
public static Rss CreateSample()
{
Rss rss = new Rss();
RssChannel channel = new RssChannel()
{
Title = "Title",
Link = "Link",
Description = "Description",
Language = "zh-cn",
LastBuildDate = DateTime.Now.ToString(),
PubDate = DateTime.Now.ToString(),
TTL = "60"
};
channel.Items.Add(new RssItem()
{
Title = "Title1",
Link = "Link",
Author = "Author",
PubDate = DateTime.Now.ToString(),
Guid = "?",
Description = "Description"
});
channel.Items.Add(new RssItem()
{
Title = "Title2",
Link = "Link",
Author = "Author",
PubDate = DateTime.Now.ToString(),
Guid = "?",
Description = "Description"
});
channel.Items.Add(new RssItem()
{
Title = "Title3",
Link = "Link",
Author = "Author",
PubDate = DateTime.Now.ToString(),
Guid = "?",
Description = "Description"
});
rss.Channel = channel;
return rss;
}
}
}
使用到的几个附加函数
public class PageFetcher
{
// 获取网页文本
public static string FetchPage(string url)
{
String result;
HttpWebRequest request = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Encoding enc = System.Text.Encoding.UTF8
using (StreamReader sr = new StreamReader(response.GetResponseStream(), enc))
{
result = sr.ReadToEnd();
sr.Close();
}
return result;
}
}
public class SerializationHelper
{
// object -> xml string
public static string SaveXmlString(object obj) {return SaveXmlString(obj, null); }
public static string SaveXmlString(object obj, XmlAttributeOverrides overrides) {return SaveXmlString(obj, obj.GetType(), overrides);}
public static string SaveXmlString(object obj, Type type, XmlAttributeOverrides overrides)
{
System.IO.MemoryStream stream = new MemoryStream();
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type, overrides);
xs.Serialize(writer, obj);
writer.Close();
}
string str = UnicodeEncoding.UTF8.GetString(stream.GetBuffer());
return str;
}
// xml string -> obj
public static object LoadXmlString(string xmlString, Type type) {return LoadXmlString(xmlString, type, null);}
public static object LoadXmlString(string xmlString, Type type, XmlAttributeOverrides overrides)
{
byte[] bytes = UnicodeEncoding.UTF8.GetBytes(xmlString);
System.IO.MemoryStream stream = new MemoryStream(bytes);
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type, overrides);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
}
}
测试代码:
Rss rss = Rss.CreateSample();
Console.WriteLine(rss.SaveXmlString());
string url = "http://www.cnblogs.com/rss";
rss = Rss.GetRss(url);
转载请注明出处:http://surfsky.cnblogs.com