XML,XDocument.Parse和foreach XElement

时间:2013-08-25 00:01:09

标签: c# xml

我今天为我的C#应用​​程序制作了XML文件 XML(我推荐使用pastebin,'因为它有点大的xml代码,对不起):Pastebin
以及C#

中的代码
private void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        XDocument doc = XDocument.Parse(e.Result);

        foreach (XElement xe in doc.Root.Element("builds").Element("build").Element("items").Elements("item"))
        {
            string s0 = xe.Element("name").ToString();
            string s1 = xe.Element("uri").ToString();
            string[] s2 = new string[2];

            s2[0] = s0;
            s2[1] = s1;
            ListViewItem lvi = new ListViewItem(s2);
            listView1.Items.Add(lvi);
        }

    }
}

我收到错误 - Object reference not set to an instance of an object.或有时根本没有显示

1 个答案:

答案 0 :(得分:0)

XML文件中没有items个节点或item个节点,因此您将获得null。