2

I am trying to serialize an object that contains an EventArgs object. If I ignore the EventArgs object upon serialization, everything works fine, but if I don't put an [XmlIgnore] above it, the application crashes with the error message

System.InvalidOperationException was unhandled
  Message=There was an error generating the XML document.
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
       at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces)
       at PullListTesting.SerializeXmlString.ToXml(Object Obj, Type ObjType) in D:\Workspace\MouseKeyboardLibrary\PullListTesting\SerializeXmlString.cs:line 65
       at PullListTesting.frmPickCapture.btnExport_Click(Object sender, EventArgs e) in D:\Workspace\MouseKeyboardLibrary\PullListTesting\frmPickCapture.cs:line 632
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       ...

Here is my class that I'm serializing, and the way that I am serializing it:

// <summary>
/// Series of events that can be recorded any played back
/// </summary>
[Serializable]
public class MacroEvent
{
    [XmlAttribute("event-type")]
    public MacroEventType MacroEventType = MacroEventType.NullEvent;

    public EventArgs EventArgs = null;
    public int TimeSinceLastEvent = 0;
    public String Value = null;

    public MacroEvent(){}

    public MacroEvent(MacroEventType macroEventType)
    {
        MacroEventType = macroEventType;
        TimeSinceLastEvent = 1;
    }

    public MacroEvent(MacroEventType macroEventType, EventArgs eventArgs, int timeSinceLastEvent)
    {

        MacroEventType = macroEventType;
        EventArgs = eventArgs;
        TimeSinceLastEvent = timeSinceLastEvent;
    }
}

public static string ToXml(object Obj, System.Type ObjType)
        {

            XmlSerializer ser;
            ser = new XmlSerializer(ObjType, SerializeXmlString.TargetNamespace);
            MemoryStream memStream;
            memStream = new MemoryStream();
            XmlTextWriter xmlWriter;
            xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            xmlWriter.Namespaces = true;
            // VVVVVVV This is where the exception occurs <-------
            ser.Serialize(xmlWriter, Obj, SerializeXmlString.GetNamespaces());
            // ^^^^^^^   This is where the exception occurs <------
            xmlWriter.Close();
            memStream.Close();
            string xml;
            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return xml;

        }

How should I go about getting the EventArgs object to serialize correctly?

ashurexm
  • 6,209
  • 3
  • 45
  • 69
  • I suspect that your problem lies elsewhere, when I try your code I get a perfectly good XML string back, this is without using XmlIgnore. Try constructing a minimal test case (which is what I did, using your code) in a console app and see what happens. These are the changes that I made: I had to create my own MacroEventType enum, mine only contained NullEvent. I new up my MacroEvent with this. I removed the namespaces from the serialization because I didn't have access to your SerializeXmlString class. – nrkn Oct 27 '10 at 23:09
  • I concur, the code runs fine. Could you add the call to ToXml? Maybe you're not really serializing an EventArgs but a child class. – Pieter van Ginkel Oct 28 '10 at 11:14
  • The error happens when EventArgs is not null. For instance, if you took a keyboard event and captured the EventArgs it would choke, but if you took a NullEvent with no EventArgs it would process fine. – ashurexm Oct 29 '10 at 02:29
  • you are not alone: http://bytes.com/topic/c-sharp/answers/546895-cannot-serialize-buildeventargs-eventargs – Cheeso Nov 07 '10 at 15:33

0 Answers0