1

我有这个链接http://en.wikipedia.org/w/api.php?format=xml&action=query&titles=panadol&prop=revisions&rvprop=content

我需要获取标签内的内容。所以我使用了这段代码

私人无效HttpsCompleted(对象发送者,Dow​​nloadStringCompletedEventArgs e){

                    WebClient wwc = new WebClient();
                    String xmlStr = "http://en.wikipedia.org/w/api.php?format=xml&action=query&titles=" + medName + "&prop=revisions&rvprop=content";
                    wwc.DownloadStringCompleted += wwc_DownloadStringCompleted;
                    wwc.DownloadStringAsync(new Uri(xmlStr));


            }
            else
            {
                MessageBox.Show("Couldn't search for medicine!\nCheck the internet connection.");
            }
        }
        catch (Exception)
        {
            // do nothing
        }

    }

也调用了这个方法。

                XNamespace ns = "http://www.w3.org/2005/Atom";
                var entry = XDocument.Parse(e.Result);
                var xmlData = new xmlWiki();
                var g = entry.Element(ns + "rev").Value.ToString();
            }
        }
        catch (Exception f)
        {
            MessageBox.Show(f.ToString());
        }
    }

但是当代码执行“var g = entry.Element(ns + "rev").Value.ToString();”时我得到空引用异常

请任何帮助。先感谢您

4

1 回答 1

0

rev不是树根的孩子。这是它的路径:

api
  query
    pages
      page
        revisions
          rev

你可以用.Descendants()它来达到它。

var entry = XDocument.Parse(html);
var g = entry.Descendants("rev").First().Value;
于 2014-04-26T05:37:31.443 回答