I have this code to read XML file from isolated storage to 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;
}
}
But when I execute it against this 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>
I have this error:
NullReferenceException
In this code fragment:
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
};
Do you know what can help?
<?xml version="1.0" encoding="utf-8" ?>
<people>
<person id="2" name="przyklad" price="850" deadline="22.10.12" beneficiary="asdasd" description="xxx" />
</people>
There is no index
attribute on your XML, so following line is the reason of you exception:
Index = c.Attribute("index").Value