1

我从这个 XML 中提取了一个内容节点。

我使用此代码提取内容节点,但我需要提取所有内容节点。

我能怎么做?

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += HttpsCompleted;
        wc.DownloadStringAsync(new Uri("http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=dj0yJmk9TU1hbThtN0IwRjYzJmQ9WVdrOVMzWjVNR05GTXpBbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1kYg--YahooDemo&query=cars"), UriKind.RelativeOrAbsolute);            
    }

    private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
            if (e.Error == null)
            {
                XNamespace ns = "urn:yahoo:answers";
                var Content = xdoc.Descendants(ns + "Content").FirstOrDefault();
                MessageBox.Show(Content.Value);
            }
        }
        else
        {
            MessageBox.Show(e.Error.Message);
        }                   
    }
4

1 回答 1

2
 var contents = xdoc.Descendants(ns + "Content");

 foreach(var content in contents){
    MessageBox.Show(content.Value);
  }
于 2013-09-02T13:05:14.457 回答