0

I'm getting this error when trying to serialize an object in C#

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll

Additional information: There was an error reflecting type 'ComboGen.ElementLocation'.

What I'm trying to do is serialize all the control elements in a form so I can read them back in and repopulate the form.

My class that I want to serialize and holds the control elements is defined like

[System.Serializable]
[System.Xml.Serialization.XmlInclude(typeof(ElementLocation))]
public class ElementLocation
{
    public List<ElementLocation> subElements;
    public TextBox ActionName;
    public TextBox CoolDown;
    public TextBox ClearInputBuffer;
    public Button AddCombo;
}

To serialize it I'm using

                System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(myStream);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ElementLocation));

                foreach (ElementLocation elemLoc in elementLocations)
                {
                    serializer.Serialize(xmlWriter, elemLoc);
                }
                myStream.Close();

But it throws an exception when trying to call Serialize. Can someone tell me what I'm doing wrong here?

Thundercleez
  • 327
  • 1
  • 4
  • 18
  • All properties in your class must be serializable too, is the `ElementLocation` serializable too, the same for the `Control` class ? – Benzara Tahar May 09 '15 at 21:28
  • Doesn't adding [System.Serializable] to the start of the class make is serializable? I realized Control was an ADT so I tried switched to using the derived classes, but I get the same error. – Thundercleez May 09 '15 at 21:30
  • Yes adding [System.Serializable] annotation before the class declaration to make it serializable, What do you mean by ADT? – Benzara Tahar May 09 '15 at 21:32
  • ADT means abstract data type. They are classes that can't be instantiated like an abstract class or interface. I thought Control was one, but it looks like it isn't. – Thundercleez May 09 '15 at 21:35
  • are you trying to serialize the System.Windows.Controls.Control class? or Control is your defined class? – Benzara Tahar May 09 '15 at 21:43
  • for the first case, check this out http://stackoverflow.com/questions/10116354/how-to-serialize-controls – Benzara Tahar May 09 '15 at 21:43

0 Answers0