如何在Windows Phone 8中创建空xml

时间:2013-10-28 10:59:11

标签: c# xml windows-phone-8 xml-serialization

我在我的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");后添加了这一行,但我仍然得到相同的例外。

1 个答案:

答案 0 :(得分:0)

我们有

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

因此您的序列化程序适用于另一种类型(DataModel)。它不会处理Dictionary<>