Reputation: 2850
Hopefully this question isn't too obvious, however I'm taking my first steps into the topic of serialization and couldn't find an explanation for the following behaviour:
I wanted to serialize a class to test if I set up everything correctly. For this I took the code from this tutorial and adapted it as follows:
private void SerializePresets(string path)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyClass));
using (TextWriter writer = new StreamWriter(path))
{
xmlSerializer.Serialize(writer, this);
}
}
This method lies within MyClass
and is also called from there. This gives me the following exception:
An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code
Additional information: There was an error reflecting type 'MyClass'.
Since MyClass
holds other class object as properties first I thought I have to make those serializabel too, however the exception still persists.
So, my guess is, that it is impossible serialize this
, however I couldn't find a confirmation to this guess.
EDIT: This property causes the issue according to the inner exception:
[XmlArray("VolumePresetList"), XmlArrayItem(typeof(LinearAxisColorPresetsModel), ElementName = "VolumePresetList")]
public ObservableCollection<LinearAxisColorPresetsModel> VolumePresetList { get; set; }
Upvotes: 0
Views: 521
Reputation: 2850
With the help of the inner exceptions (thanks for the tip again) I could find out the reason why the serialization failed.
The class LinearAxisColorPresetsModel
did not have a parameterless Constructor, which caused this issue.
Simply adding
/// <summary>
/// Default Constructor
/// </summary>
private LinearAxisColorPresetsModel()
{
}
to this class solved the problem for me. What remains is to find out the reason, why we must have a parameterless Constructor.
EDIT: Found the reasoning behind this behaviour in this post.
Upvotes: 1
Reputation: 34421
You can use this. It must be one of the properties like a Dictionary that doesn't serialize. See my example below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication103
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.Serialize(FILENAME);
}
}
public class MyClass
{
public string test { get; set; }
public void Serialize(string filename)
{
SerializePresets(filename);
}
private void SerializePresets(string path)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyClass));
using (TextWriter writer = new StreamWriter(path))
{
xmlSerializer.Serialize(writer, this);
}
}
}
}
Upvotes: 1