开发者

C# Serialize EventArgs to Xml

开发者 https://www.devze.com 2023-01-22 12:15 出处:网络
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 appli

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.Get开发者_运维百科Buffer());
            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?

0

精彩评论

暂无评论...
验证码 换一张
取 消