[英]Reading XML from isolated storage to ListBox
我有以下代碼可以將XML文件從隔離存儲讀取到ListBox:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Open, isoStore))
{
XDocument loadedCustomData = XDocument.Load(isoStream);
var filteredData = from c in loadedCustomData.Descendants("person")
select new Person()
{
Name = c.Attribute("name").Value,
Beneficiary = c.Attribute("beneficiary").Value,
Price = c.Attribute("price").Value,
Deadline = c.Attribute("deadline").Value,
Index = c.Attribute("index").Value,
Description = c.Attribute("description").Value
};
listBox.ItemsSource = filteredData;
}
}
但是當我針對這個XML執行它時
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person id="2"
name="przyklad"
price="850"
deadline="22.10.12"
beneficiary="asdasd"
description="xxx" />
</people>
我有這個錯誤:
NullReferenceException
在此代碼片段中:
select new Person()
{
Name = c.Attribute("name").Value,
Beneficiary = c.Attribute("beneficiary").Value,
Price = c.Attribute("price").Value,
Deadline = c.Attribute("deadline").Value,
Index = c.Attribute("index").Value,
Description = c.Attribute("description").Value
};
你知道有什么可以幫助的嗎?
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person id="2" name="przyklad" price="850" deadline="22.10.12" beneficiary="asdasd" description="xxx" />
</people>
XML上沒有index
屬性,因此以下行是導致異常的原因:
Index = c.Attribute("index").Value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.