c#取得Rssデータ
7596 ワード
public class Channel
{
private string _title;
private string _link;
private string _description;
private List<Item> items = new List<Item>();
#region
/**//// <summary>
///
/// </summary>
public string title
{
get{return _title;}
set{_title = value.ToString();}
}
/**//// <summary>
///
/// </summary>
public string link
{
get{return _link;}
set{_link = value.ToString();}
}
/**//// <summary>
///
/// </summary>
public string description
{
get{return _description;}
set{_description = value.ToString();}
}
public List <Item > Items
{
get { return items; }
}
#endregion
public Channel(){}
}//
public class Feed
{
private string _url;
private System.DateTime _lastModified;
private System.DateTime _lastRssDate;
private Channel channel = new Channel();
#region
public string url
{
get{return _url;}
set{_url=value;}
}
public System.DateTime lastModified
{
get{return _lastModified;}
}
public System.DateTime lstRssDate
{
set{_lastRssDate=value;}
}
public Channel Channel
{
get { return channel; }
}
#endregion
public Feed()
{
}
public Feed(string url,System.DateTime dt)
{
this._url=url;
this._lastRssDate=dt;
}
public void Read()
{
XmlDocument xDoc=new XmlDocument();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.Timeout=15000;
request.UserAgent=@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";
Stream stream;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
this._lastModified = response.LastModified;
stream = response.GetResponseStream();
StreamReader sr;
//System.Xml.XmlReader = new XmlReader();
//stream=Encoding.Convert(Encoding.GetEncoding("GBK"),Encoding.GetEncoding("gb2312"),Convert.ToSByte(stream));
if(this.Get_CH(response.Headers["Content-Type"].ToString())=="GBK")
{
sr= new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));
xDoc.Load(sr);
}
else
{
// sr= new StreamReader(stream,System.Text.Encoding.UTF8);
xDoc.Load(stream);
}
if(this._lastRssDate<this._lastModified)
{
XmlNodeList xnList=xDoc.DocumentElement["channel"].SelectNodes("item");
// XmlNodeList xnList=xDoc.SelectNodes("items");
int a= xnList.Count;
foreach(XmlNode xNode in xnList)
{
Item rt=new Item();
rt.title=xNode.SelectSingleNode("title").InnerText.Replace("'","''");
rt.link=xNode.SelectSingleNode("link").InnerText.Replace("'","''");
try
{
rt.description = xNode.SelectSingleNode("description").InnerText.Replace("'", "''");
}
catch { }
try
{
rt.pubDate=xNode.SelectSingleNode("pubDate").InnerText;
}
catch
{
rt.pubDate=this._lastModified.ToString();
}
channel.Items.Add(rt);
}
}
}
public string Create()
{
return "";
}
private string Get_CH(string s)
{
int l=s.IndexOf("charset=")+8;
return s.Substring(l,s.Length-l);
}
}//
public class Item
{
private string _title="";
private string _link="";
private string _description="";
private string _pubDate="";
#region
/**//// <summary>
///
/// </summary>
public string title
{
get{
if (_title.Length > 21)
{
return _title.Substring(0, 21)+"...";
}
else
{return _title; }
}
set{
_title=value.ToString();}
}
/**//// <summary>
///
/// </summary>
public string link
{
get{return _link;}
set{_link=value.ToString();}
}
/**//// <summary>
///
/// </summary>
public string description
{
get{return _description;}
set
{
if (value != null)
{ _description = value; }
else
{ _description = ""; }
}
}
/**//// <summary>
///
/// </summary>
public string pubDate
{
get{return _pubDate;}
set{_pubDate=C_Date(value);}
}
#endregion
public Item(){}
private string C_Date(string input)
{
System.DateTime dt;
try
{
dt=Convert.ToDateTime(input);
}
catch
{
dt=System.DateTime.Now;
}
return dt.ToString();
}
string GetDate(DateTime dt)
{ string House = dt.ToString("HH");
int hour = int.Parse(House);
int mine = dt.Minute;
int second = dt.Second;
string AP = "";
if(hour >12)
{
AP=" ";
hour = hour - 12;
}
else
{AP=" ";}
return string.Format("{0} {1} {2}", dt.ToString ("yyyy/MM/dd"),AP,hour +":"+mine +":"+second );
}
public string DateString
{
get
{
DateTime time = Convert.ToDateTime(pubDate);
return GetDate(time);
}
}
public DateTime PuDate
{
get { return Convert.ToDateTime(pubDate); }
}
}//