简体   繁体   English

从隔离存储读取XML到ListBox

[英]Reading XML from isolated storage to ListBox

I have this code to read XML file 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;
            }
        }

But when I execute it against this XML 但是当我针对这个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: XML上没有index属性,因此以下行是导致异常的原因:

Index = c.Attribute("index").Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM