public MainWindow() { InitializeComponent(); try { // 取得するRSSのURL string rss = "https://news.yahoo.co.jp/rss/topics/it.xml"; //XElementのLoadメソッドにURLを渡して情報を取得する //XElementのリファレンス:https://docs.microsoft.com/ja-jp/dotnet/api/system.xml.linq.xelement?view=net-5.0 XElement element = XElement.Load(rss); //"channel"エレメントを取得 XElement channelElement = element.Element("channel"); //RssInfoクラスのインスタンスを生成 RssInfo rssInfo = new RssInfo(); //ここではヘッダ情報を取得している //それぞれのメンバ変数へ取得したchannelエレメントの中から指定したエレメントの中身を取得 rssInfo.Title = channelElement.Element("title").Value; rssInfo.Description = channelElement.Element("description").Value; rssInfo.Link = channelElement.Element("link").Value; rssInfo.PubDate = channelElement.Element("pubDate").Value; //Itemエレメントを全て取得する //<Item> // <item1> //</Item> //<Item> // <item2> //</Item> //<item1><item2>が取得される IEnumerable <XElement> elementItems = channelElement.Elements("item"); //ListをRssItemInfoクラス型で作成する List <RssItemInfo> rssItemInfos = new List <RssItemInfo>(); //elementItemsの中身を一つずつ取得 foreach (XElement elmItem in elementItems) { RssItemInfo itemInfo = new RssItemInfo(); //それぞれのElementに含まれる値を取得してくる itemInfo.Title = elmItem.Element("title").Value; itemInfo.Description = elmItem.Element("description").Value; itemInfo.Link = elmItem.Element("link").Value; itemInfo.PubDate = elmItem.Element("pubDate").Value; rssItemInfos.Add(itemInfo); } //タイトルを取得して文字列型に格納 string rsstext = ""; foreach (var work in rssItemInfos) { //タイトルを文字列結合してTextBlockへ反映 rsstext += work.Title + "\n"; //ボタンの拡張クラスを作って押すと別ウィンドウに情報を出すようにする CustomButton bt = new CustomButton(); bt.Content = work.Title; bt.rii = work; bt.Height = 100; bt.FontSize = 36; //クリックイベントのイベントハンドラを設定 bt.Click += new RoutedEventHandler(CustomButton_Click); stack.Children.Add(bt); } //textblock.Text = rsstext; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }
public CustomButton() { rii = new RssItemInfo(); }