问题
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