How to create an empty xml in Windows Phone 8

不问归期 提交于 2019-12-13 04:40:36

问题


I'm creating an empty xml file in my WP8 app like this

    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;
    }

But I get a System.InvalidOperationException on the line serializer.Serialize(stream, __data);

What am I doing wrong?

Edit: I added this line after creating the new dictionary __data.Add("testkey", "testdatavalue"); but I still get the same exception.


回答1:


We have

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

So your serializer is for another Type (DataModel). It won't handle a Dictionary<>.



来源:https://stackoverflow.com/questions/19632942/how-to-create-an-empty-xml-in-windows-phone-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!