0

我正在像这样在我的 WP8 应用程序中创建一个空的 xml 文件

    public static bool create()
    {
        Dictionary<string, object> __data = new Dictionary<string, object>();
        XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
        xmlWriterSettings.Indent = true;

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                {
                    serializer.Serialize(stream, __data);
                }
            }
        }
        return true;
    }

但我得到一个System.InvalidOperationException就行了serializer.Serialize(stream, __data);

我究竟做错了什么?

编辑:我在创建新字典后添加了这一行,__data.Add("testkey", "testdatavalue");但我仍然得到同样的异常。

4

1 回答 1

0

我们有

Dictionary<string, object> __data = ...
XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
serializer.Serialize(stream, __data);

所以你的序列化器是另一个类型(DataModel)。它不会处理Dictionary<>.

于 2013-10-28T11:29:39.367 回答