C# 将 EventArgs 序列化为 Xml

发布于 2024-09-30 04:40:12 字数 3028 浏览 3 评论 0原文

我正在尝试序列化包含 EventArgs 对象的对象。如果我在序列化时忽略 EventArgs 对象,则一切正常,但如果我不在其上方放置 [XmlIgnore] ,应用程序将崩溃并显示错误消息

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 ,以及我序列化它的方式:

// <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;

        }

我应该如何让 EventArgs 对象正确序列化?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文