将特定类型数组序列化/反序列化为xml

时间:2017-08-02 07:04:22

标签: c# xml wpf serialization exception-handling

我在序列化/反序列化特定对象数组时遇到了问题。

[System.SerializableAttribute()] 
public class ctyp_HT
{
   private Operationalmessage[] opm;
   [System.Xml.Serialization.XmlElementAttribute("OpMessages", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
   public Operationalmessage[] P_OpMessages{
      get {
          return this.opm;
      }
      set {
          this.opm= value;
      }
   }
}

在此属性中使用set {},serialize进程在运行时崩溃。没有set {}它会起作用,但为什么呢。该类标记为[System.SerializableAttribute()]。 这是我与一般函数结合使用的函数调用:

SerializeObject<ctyp_HT>(objInstance);

public static string SerializeObject<T>(T serializableObject)
    {
        if (serializableObject == null) { return string.Empty; }

        try
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, serializableObject);
                stream.Position = 0;
                xmlDocument.Load(stream);
                stream.Close();
            }
            return xmlDocument.InnerXml;
        }
        catch (Exception ex)
        {
            return (ex.Message + "\n" + ex.InnerException.Message + "\n" + ex.StackTrace + "\n\n\n" + ex.InnerException.StackTrace);
        }
    }

我做错了什么?求助。

首次评论后编辑:

[System.SerializableAttribute()]
public class Operationalmessage : I_Information
{
   private object objGen;
[System.Xml.Serialization.XmlElementAttribute("OPEN", typeof(ctyp_intEndlage_AUF), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("CLOSE", typeof(ctyp_intEndlage_ZU), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("BETWEEN", typeof(ctyp_intEndlage_ZWISCHEN), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("objIdentifier")]
   public object P_objGen
  {
     get { return objGen; }
     set { objGen = value; }
  }
  private UnnamedChoice0 objIdentifier;
  [System.Xml.Serialization.XmlIgnoreAttribute()]
  public UnnamedChoice0 P_objIdentifier
  {
     get { return objIdentifier; }
     set { objIdentifier = value; }
  }
  [System.Xml.Serialization.XmlIgnoreAttribute()]
  public string P_Info
  {
     get
     {
        if(objGen is I_Information) return objIdentifier + "(" + ((I_Information)objGen).P_Info + ")"; 
        else return objIdentifier + "(" + objGen + ")";
     }
  }
}

unnamedchoice是一个公共枚举。由于我的生成器,ctyp_intEndlage_Auf / ZU / Zwischen类具有不同的名称。

    [System.SerializableAttribute()]
public class ctyp_intEndlage_AUF : I_Information
{
   private ctyp_Infoquelle_BOOL1_In Informationsquelle = null;
   [System.Xml.Serialization.XmlElementAttribute("Informationsquelle", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
   public ctyp_Infoquelle_BOOL1_In P_Informationsquelle
   {
      get { return Informationsquelle; }
      set { Informationsquelle = value; }
   }
   private string Extension = "036";
   [System.Xml.Serialization.XmlElementAttribute("Extension", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
   public string P_Extension
   {
      get { return Extension; }
      set { Extension = value; }
   }
   public string P_Info
   {
      get 
      {
         string result = "";         return "( " + Informationsquelle.P_Info + " "  + Extension + " "  + result + ")"; 
      }
   }
   public ctyp_intEndlage_AUF() 
   {
      Informationsquelle = new ctyp_Infoquelle_BOOL1_In();
   }
}

和最后一堂课bool1

    [System.SerializableAttribute()]
public class ctyp_Infoquelle_BOOL1_In : I_Information
{
   private object objGen;
   [System.Xml.Serialization.XmlElementAttribute("HardwareEingang", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
   [System.Xml.Serialization.XmlElementAttribute("KoppelEingang", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
   [System.Xml.Serialization.XmlChoiceIdentifierAttribute("objIdentifier")]
   public object P_objGen
   {
      get { return objGen; }
      set { objGen = value; }
   }
   private UnnamedChoice1 objIdentifier;
   [System.Xml.Serialization.XmlIgnoreAttribute()]
   public UnnamedChoice1 P_objIdentifier
   {
      get { return objIdentifier; }
      set { objIdentifier = value; }
   }
   public string P_Info
   {
      get
      {
         if(objGen is I_Information) return objIdentifier + "(" + ((I_Information)objGen).P_Info + ")"; 
         else return objIdentifier + "(" + objGen + ")";
      }
   }
   public ctyp_Infoquelle_BOOL1_In() 
   {
   }
}

属性P_Info在I_Information接口中声明。 Sry所有的德国名字=)。

1 个答案:

答案 0 :(得分:1)

我使用正确的代码创建.net小提琴:https://dotnetfiddle.net/67evia

您的问题是在"-Dcom.sun.management.jmxremote", \ "-Dcom.sun.management.jmxremote.port=9010", \ "-Dcom.sun.management.jmxremote.rmi.port=9010", \ "-Dcom.sun.management.jmxremote.local.only=false", \ "-Dcom.sun.management.jmxremote.authenticate=false", \ "-Dcom.sun.management.jmxremote.ssl=false", \ 课程中使用私人字段。

你有:

System.Xml.Serialization.XmlChoiceIdentifierAttribute

必须是:

[System.Xml.Serialization.XmlChoiceIdentifierAttribute("objIdentifier")]

我还在serilizer方法中扩展了错误记录,以正确读取所有异常。

[System.Xml.Serialization.XmlChoiceIdentifierAttribute("P_objIdentifier")]