C# 将特定类型数组序列化/反序列化为xml
我遇到了一个关于序列化/反序列化特定对象数组的问题C# 将特定类型数组序列化/反序列化为xml,c#,xml,wpf,serialization,exception-handling,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 Operat
[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{},序列化进程将在运行时崩溃。没有set{}它会工作,但为什么呢。该类被标记为[System.SerializableAttribute()]。
下面是我与泛型函数结合使用的functioncall:
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);
}
}
未命名选项是一个公共枚举。由于我的生成器,类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_信息接口中声明。
Sry代表所有德语名称=)。我已经用正确的代码创建了.net fiddle: 您的问题是在
System.Xml.Serialization.XmlChoiceIdentifierAttribute
类中使用私有字段
你有:
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("objIdentifier")]
它必须是:
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("P_objIdentifier")]
我还扩展了serilizer方法中的错误日志记录,以正确读取所有异常
catch (Exception ex)
{
var log = (ex.Message + "\n" + ex.StackTrace );
var extemp = ex.InnerException;
while (extemp != null)
{
log += "\n\n\n" + extemp.Message + "\n" + extemp.StackTrace;
extemp = extemp.InnerException;
}
return log;
}
你能把
信息包括进来吗?没有代码很难判断出什么地方出了问题。顺便说一句:对于要使用XmlArray
和XmlArrayItem
属性而不是xmlement
的数组(除非您不想使用元素包装集合)。序列化时,属性必须是公共的,而不是私有的。非常感谢您对我的问题进行的深入调查。我想我是瞎的,但是错误信息并没有帮我解决问题。问题解决了,非常感谢!附录:这种扩展的异常日志记录是我需要的工具。
catch (Exception ex)
{
var log = (ex.Message + "\n" + ex.StackTrace );
var extemp = ex.InnerException;
while (extemp != null)
{
log += "\n\n\n" + extemp.Message + "\n" + extemp.StackTrace;
extemp = extemp.InnerException;
}
return log;
}