我正在尝试将XML文件中的所有节点添加到listView中,但我做错了什么,即使查看了很多示例,也无法找出解决方法。这是XML片段:
<queue>
<slots>
<slot>
<status>Downloading</status>
<filename>file1</filename>
<size>1 GB</size>
</slot>
<slot>
<status>Downloading</status>
<filename>file2</filename>
<size>2 GB</size>
</slot>
</slots>
</queue>
这是代码:
XDocument xDoc = XDocument.Load(xmlFilePath);
List<Download> list = new List<Download>();
foreach (var download in xDoc.Descendants("slots"))
{
string filename = download.Element("filename").Value;
string size = download.Element("size").Value;
string status = download.Element("status").Value;
list.Add(new Download { Filename = filename, Size = size, Status = status });
}
一如既往地感谢您的帮助。
编辑:为了澄清,我在以下代码中遇到了NullReferenceException:
string filename = download.Element("filename").Value;
我知道ListView还没有完成,我还没有处理这一部分 :)