I try to save an object to an XML Document. I always get an inner Exception Error. I think it has to do something with boxing and unboxing but i cannot help myself.
So here we go:
The Method for XML Saving
public class Saveclass
//
public static void Save(string filename, List<Class> classlist)
{
StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8);
XmlSerializer serializer = new XmlSerializer(typeof(class));
serializer.Serialize(writer,classlist);//Here i get the error at classlist
writer.Close();
}
And here the SaveFile Dialog
private void SaveButton(object sender, EventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "XML Data|*.xml";
if (dialog.ShowDialog() == DialogResult.OK)
{
List<class> classlist = null;
foreach (ListViewItem item in this.listViewWinforms.Items)
{
classlist = new List<class>();
}
Saveclass.Save(dialog.FileName, classlist)
}
}
In the Basic i have a Listview with Items inside and want to save these Items in my listview to an XML Document Error is
System.InvalidOperationException: 'Trying to generate an XML Document
InvalidCastException: The object of the Type "System.Collections.Generic.List`1[Namespace.Class]" cannot cast into"Namespace.Class"