2011-11-14 19 views
3

Windows Phone 7アプリケーションでフィードXML(RSS)を使用し、その情報をListBoxに表示する必要があります。RSSフィードを使用するXMLと表示情報

は、私はXMLフィード内のコンテンツを読み取ろうとした方法に従い:

private void button1_Click(object sender, RoutedEventArgs e) 
    {    
      client.DownloadStringAsync(new Uri("http://earthquake.usgs.gov/eqcenter/recenteqsww/catalogs/eqs7day-M2.5.xml"), "usgs"); 
    } 

誰かがXML情報を取得するために進むこととListBoxの項目としてそれらを表示する方法を私を導いてくださいことはできますか?

答えて

5

あなたは二つのことを行う必要があります:

  1. をURLからフィードXMLをダウンロードして、あなたは
  2. そこを持って、次のコードをXMLを解析し、結果のXML文書に

を処理する方法を示していますそれを行うには:

GetFeedはパート1、handleFeedはパート2、button1_Clickはスターのクリックハンドラです

// this method downloads the feed without blocking the UI; 
// when finished it calls the given action 
public void GetFeed(Action<string> doSomethingWithFeed) 
{ 
    HttpWebRequest request = HttpWebRequest.CreateHttp("http://earthquake.usgs.gov/eqcenter/recenteqsww/catalogs/eqs7day-M2.5.xml"); 
    request.BeginGetResponse(
     asyncCallback => 
     { 
      string data = null; 

      using (WebResponse response = request.EndGetResponse(asyncCallback)) 
      { 
       using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
       { 
        data = reader.ReadToEnd(); 
       } 
      } 
      Deployment.Current.Dispatcher.BeginInvoke(() => doSomethingWithFeed(data)); 
     } 
     , null); 
} 

// this method will be called by GetFeed once the feed has been downloaded 
private void handleFeed(string feedString) 
{ 
    // build XML DOM from feed string 
    XDocument doc = XDocument.Parse(feedString); 

    // show title of feed in TextBlock 
    textBlock1.Text = doc.Element("rss").Element("channel").Element("title").Value; 
    // add each feed item to a ListBox 
    foreach (var item in doc.Descendants("item")) 
    { 
     listBox1.Items.Add(item.Element("title").Value); 
    } 

    // continue here... 
} 

// user clicks a button -> start feed download 
private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    GetFeed(handleFeed); 
} 

ほとんどのエラーチェックは省略されています。期待するXML要素についての情報は、Wikipediaです。 XMLファイルをダウンロードするコードは、HttpWebRequestを使用すると約this excellent blog postに基づいています。この記事では

0
+2

なお、(http://meta.stackoverflow.com/tags/link-only-answers/info)が推奨され、[リンクのみの回答]、SOの回答解決策の検索の終点でなければなりません(これは、時間の経過とともに陳腐化する傾向のある参照の途中停止です)。リンクを参考にして、ここにスタンドアロンの概要を追加することを検討してください。 – kleopatra

+0

これは答えよりむしろコメントでなければなりません。 –

関連する問題