简体   繁体   English

WP7-在隔离存储中反序列化XML文件

[英]WP7 - Deserializing a XML file in isolated storage

I am creating a application in Windows Phone 7 Mango, and when I load up the application, the MainViewModel loads all the info from a file in isolated storage, with this function: 我正在Windows Phone 7 Mango中创建一个应用程序,当我加载该应用程序时,MainViewModel使用以下功能从隔离存储中的文件加载所有信息:

 private ObservableCollection<KasutajadViewModel> LoadUsers()
    {
        ObservableCollection<KasutajadViewModel> kasutajad = new ObservableCollection<KasutajadViewModel>();
        try
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Kasutajad.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<PiletViewModel>));
                    //ObservableCollection<KasutajadViewModel> data
                    kasutajad =
                        (ObservableCollection<KasutajadViewModel>)serializer.Deserialize(stream);
                    stream.Close();
                }
            }
        }
        catch (Exception)
        {

        }
        return kasutajad;
    }

Problem is, that the deserializer doesn't return the data. 问题是,解串器不会返回数据。 Even when the XML file it reads from is like this: 即使从中读取XML文件也是如此:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfKasutajadViewModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <KasutajadViewModel>
    <Nimi>Reigo Hein</Nimi>
    <Isikukood>96952422597</Isikukood>
 </KasutajadViewModel>
</ArrayOfKasutajadViewModel>       

That is produced by a SaveUsers function, but I think this is redundant for the problem. 那是由SaveUsers函数产生的,但是我认为这对于这个问题是多余的。 The LoadUsers loads the stream correctly, but the deserialization doesn't output the required data. LoadUsers可以正确加载流,但是反序列化不会输出所需的数据。

Hope anyone can help me, thank you. 希望有人能帮助我,谢谢。

You're creating a XmlSerializer for an ObservableCollection of PiletViewModel , yet you're deserializing an array of KasutajadViewModel . 您正在为PiletViewModel的ObservableCollection创建一个XmlSerializer,但是正在反序列化KasutajadViewModel数组。 There's a type inconsistency here. 这里存在类型不一致。

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

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