title: C# Serialize EventArgs to Xml
body: <p>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 <code>[XmlIgnore]</code> above it, the application crashes with the error message</p>
  
  <pre><code>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&amp; m, MouseButtons button, Int32 clicks)
         ...
  </code></pre>
  
  <p>Here is my class that I'm serializing, and the way that I am serializing it:</p>
  
  <pre><code>// &lt;summary&gt;
  /// Series of events that can be recorded any played back
  /// &lt;/summary&gt;
  [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 &lt;-------
              ser.Serialize(xmlWriter, Obj, SerializeXmlString.GetNamespaces());
              // ^^^^^^^   This is where the exception occurs &lt;------
              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;
  
          }
  </code></pre>
  
  <p>How should I go about getting the EventArgs object to serialize correctly?</p>