首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 製作新聞閱讀器(二):WinForms 介面設計、RSS Channel/Item 結構定義、XmlDocument 載入與 FoundChildNode 遞迴尋找節點

     編輯:關於C語言

WinForms 介面

  • Label + TextBox (txtURL):輸入 RSS 連結
  • Button (bnRead):執行讀取
  • TreeView (treeRSS):顯示新聞列表

資料結構定義


public class rss
{
    public struct Channel
    {
        public string Title;
        public Hashtable Items;
    }
    public struct Item
    {
        public string Title;
        public string Description;
        public string Link;
    }
}

Channel.Items 以 Hashtable 儲存多筆 Item,Key 使用 Count 保證唯一。

XML 載入與節點尋找


XmlTextReader Reader = new XmlTextReader(URL);
XmlValidatingReader Valid = new XmlValidatingReader(Reader);
Valid.ValidationType = ValidationType.None;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Reader);

FoundChildNode 遞迴尋找


private XmlNode FoundChildNode(XmlNode Node, string Name)
{
    for (int i = 0; i < Node.ChildNodes.Count; i++)
    {
        if (Node.ChildNodes[i].Name == Name && Node.ChildNodes[i].ChildNodes.Count > 0)
            return Node.ChildNodes[i];
    }
    return null;
}

XmlNode rssNode = FoundChildNode(xmlDoc, "rss");
XmlNode channelNode = FoundChildNode(rssNode, "channel");
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved