现在RSS很流行,一度风行于整个互联网,的确,他给我们带来了很大的改变和方便!RSS也分很多版本,rss.1.0.rss2.0比较多,还有atom格式的!
在解析的时候给我带来了麻烦,由于本人没有怎么研究过xml和相关的解析工作,所以碰到很多问题,目前能成功解析rss2.0。
顶部的一些解析(暂时这么叫吧,我也不知道叫什么)
1
private
void
GetRssHead(XmlDocument doc)
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
//XmlDocument doc = new XmlDocument();
4
//doc.LoadXml(this.RssXmlStr);
5
XmlElement elem = (XmlElement)doc.DocumentElement.FirstChild;
6
this.headtitle=elem.SelectSingleNode("title").InnerText;
7
this.headurl=elem.SelectSingleNode("link").InnerText;
8
this.headdescription=elem.SelectSingleNode("description").InnerText;
9![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
}
内容的解析:
![None.gif](/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
private
void
ParseItems()
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
XmlDocument doc = new XmlDocument();
4
doc.LoadXml(this.RssXmlStr);
5
XmlNodeList nodeList;
6
XmlElement root = doc.DocumentElement;
7
nodeList = root.GetElementsByTagName("item");
8
for(int n=0;n<nodeList.Count;n++)
9![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
listtitle.Add(nodeList[n].SelectSingleNode ("title").InnerText);
11
listlink.Add(nodeList[n].SelectSingleNode ("link").InnerText);
12
listdescription.Add(nodeList[n].SelectSingleNode ("description").InnerText);
13
}
14
}
但是对于rss1.0我目前只能这样处理:
![None.gif](/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
//
头部
2
nodeList
=
root.GetElementsByTagName(
"
channel
"
);
3
this
.headtitle
=
nodeList[
0
].ChildNodes[
0
].InnerText;
4
this
.headurl
=
nodeList[
0
].ChildNodes[
1
].InnerText;
5
this
.headdescription
=
nodeList[
0
].ChildNodes[
2
].InnerText;
6
不能用SelectSingleNode来取元素,这样RSS变了顺序就不行了,还有Atom格式的一样也没处理好!
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
6
![None.gif](/Images/OutliningIndicators/None.gif)