属性未序列化
Properties not getting serialized
我使用 reflection.emit 生成运行时 class,class 的生成似乎很好,如 ILSpy 中所示:
using indice.Edi.Serialization;
using IQoneEDIParser.Formats;
using System;
using System.Xml.Serialization;
[XmlRoot("CONTRL"), XmlType("CONTRL")]
[Serializable]
public class CONTRL : FormatBase
{
private string _syntaxkennung;
private int _sintaxversion;
private string _absenderid;
private string _absendercodeunb;
private string _empfängerid;
private string _empfängercodeunb;
private string _dokumentdatum;
private string _dokumentzeit;
private string _datenaustauschreferenz;
[EdiValue("X(4)", "UNB/0"), XmlElement("Syntaxkennung", typeof(string))]
public string Syntaxkennung
{
get
{
return this._syntaxkennung;
}
set
{
this._syntaxkennung = value;
}
}
[EdiValue("9(1)", "UNB/0/1"), XmlElement("Sintaxversion", typeof(int))]
public int Sintaxversion
{
get
{
return this._sintaxversion;
}
set
{
this._sintaxversion = value;
}
}
[EdiValue("X(35)", "UNB/1/0"), XmlElement("AbsenderID", typeof(string))]
public string AbsenderID
{
get
{
return this._absenderid;
}
set
{
this._absenderid = value;
}
}
[EdiValue("X(4)", "UNB/1/1"), XmlElement("AbsenderCodeUNB", typeof(string))]
public string AbsenderCodeUNB
{
get
{
return this._absendercodeunb;
}
set
{
this._absendercodeunb = value;
}
}
[EdiValue("X(35)", "UNB/2/0"), XmlElement("EmpfängerID", typeof(string))]
public string EmpfängerID
{
get
{
return this._empfängerid;
}
set
{
this._empfängerid = value;
}
}
[EdiValue("X(4)", "UNB/2/1"), XmlElement("EmpfängerCodeUNB", typeof(string))]
public string EmpfängerCodeUNB
{
get
{
return this._empfängercodeunb;
}
set
{
this._empfängercodeunb = value;
}
}
[EdiValue("X(6)", "UNB/3/0"), XmlElement("Dokumentdatum", typeof(string))]
public string Dokumentdatum
{
get
{
return this._dokumentdatum;
}
set
{
this._dokumentdatum = value;
}
}
[EdiValue("X(4)", "UNB/3/1"), XmlElement("Dokumentzeit", typeof(string))]
public string Dokumentzeit
{
get
{
return this._dokumentzeit;
}
set
{
this._dokumentzeit = value;
}
}
[EdiValue("X(14)", "UNB/4/0"), XmlElement("Datenaustauschreferenz", typeof(string))]
public string Datenaustauschreferenz
{
get
{
return this._datenaustauschreferenz;
}
set
{
this._datenaustauschreferenz = value;
}
}
}
出于任何未知原因,唯一被序列化为 Xml 的 属性 是 Syntaxversion(唯一一个类型为 Integer)
这里是序列化方法:
public static String SerializeToXml(FormatBase toSerialize, Type inType)
{
XmlWriterSettings xmlSettings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true,
OmitXmlDeclaration = true,
NewLineOnAttributes = true,
CloseOutput = true
};
using (StringWriter stringWriter = new StringWriter())
{
XmlSerializer serializer2 = new XmlSerializer(inType);
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer2.Serialize(xmlWriter, toSerialize, ns);
return stringWriter.ToString();
}
}
}
关于为什么其余的属性没有被序列化有什么想法吗?提前致谢!
EDIT_1:
在进一步测试后,我复制粘贴生成的 class 并将其创建为我的项目中的 "normal" 类型,再次尝试序列化方法,并按预期工作(参见 EDIT_2)
会不会是 Reflection.Emit 问题 and/or 我的运行时程序集?
EDIT_2:
Xml 序列化的预期结果:
<CONTRL>
<Syntaxkennung>UNOC</Syntaxkennung>
<Sintaxversion>3</Sintaxversion>
<AbsenderID>9904325000003</AbsenderID>
<AbsenderCodeUNB>500</AbsenderCodeUNB>
<EmpfängerID>9900080000007</EmpfängerID>
<EmpfängerCodeUNB>500</EmpfängerCodeUNB>
<Dokumentdatum>161007</Dokumentdatum>
<Dokumentzeit>1723</Dokumentzeit>
</CONTRL>
收到:
<CONTRL>
<Sintaxversion>3</Sintaxversion>
</CONTRL>
我最终在 FormatBase (Base class) 中实现了 IXmlSerializable
public void WriteXml(XmlWriter writer)
{
foreach (PropertyInfo property in GetType().GetProperties())
{
writer.WriteElementString(property.Name, property.GetValue(this)?.ToString());
}
}
并按预期工作。
我使用 reflection.emit 生成运行时 class,class 的生成似乎很好,如 ILSpy 中所示:
using indice.Edi.Serialization;
using IQoneEDIParser.Formats;
using System;
using System.Xml.Serialization;
[XmlRoot("CONTRL"), XmlType("CONTRL")]
[Serializable]
public class CONTRL : FormatBase
{
private string _syntaxkennung;
private int _sintaxversion;
private string _absenderid;
private string _absendercodeunb;
private string _empfängerid;
private string _empfängercodeunb;
private string _dokumentdatum;
private string _dokumentzeit;
private string _datenaustauschreferenz;
[EdiValue("X(4)", "UNB/0"), XmlElement("Syntaxkennung", typeof(string))]
public string Syntaxkennung
{
get
{
return this._syntaxkennung;
}
set
{
this._syntaxkennung = value;
}
}
[EdiValue("9(1)", "UNB/0/1"), XmlElement("Sintaxversion", typeof(int))]
public int Sintaxversion
{
get
{
return this._sintaxversion;
}
set
{
this._sintaxversion = value;
}
}
[EdiValue("X(35)", "UNB/1/0"), XmlElement("AbsenderID", typeof(string))]
public string AbsenderID
{
get
{
return this._absenderid;
}
set
{
this._absenderid = value;
}
}
[EdiValue("X(4)", "UNB/1/1"), XmlElement("AbsenderCodeUNB", typeof(string))]
public string AbsenderCodeUNB
{
get
{
return this._absendercodeunb;
}
set
{
this._absendercodeunb = value;
}
}
[EdiValue("X(35)", "UNB/2/0"), XmlElement("EmpfängerID", typeof(string))]
public string EmpfängerID
{
get
{
return this._empfängerid;
}
set
{
this._empfängerid = value;
}
}
[EdiValue("X(4)", "UNB/2/1"), XmlElement("EmpfängerCodeUNB", typeof(string))]
public string EmpfängerCodeUNB
{
get
{
return this._empfängercodeunb;
}
set
{
this._empfängercodeunb = value;
}
}
[EdiValue("X(6)", "UNB/3/0"), XmlElement("Dokumentdatum", typeof(string))]
public string Dokumentdatum
{
get
{
return this._dokumentdatum;
}
set
{
this._dokumentdatum = value;
}
}
[EdiValue("X(4)", "UNB/3/1"), XmlElement("Dokumentzeit", typeof(string))]
public string Dokumentzeit
{
get
{
return this._dokumentzeit;
}
set
{
this._dokumentzeit = value;
}
}
[EdiValue("X(14)", "UNB/4/0"), XmlElement("Datenaustauschreferenz", typeof(string))]
public string Datenaustauschreferenz
{
get
{
return this._datenaustauschreferenz;
}
set
{
this._datenaustauschreferenz = value;
}
}
}
出于任何未知原因,唯一被序列化为 Xml 的 属性 是 Syntaxversion(唯一一个类型为 Integer)
这里是序列化方法:
public static String SerializeToXml(FormatBase toSerialize, Type inType)
{
XmlWriterSettings xmlSettings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Indent = true,
OmitXmlDeclaration = true,
NewLineOnAttributes = true,
CloseOutput = true
};
using (StringWriter stringWriter = new StringWriter())
{
XmlSerializer serializer2 = new XmlSerializer(inType);
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer2.Serialize(xmlWriter, toSerialize, ns);
return stringWriter.ToString();
}
}
}
关于为什么其余的属性没有被序列化有什么想法吗?提前致谢!
EDIT_1: 在进一步测试后,我复制粘贴生成的 class 并将其创建为我的项目中的 "normal" 类型,再次尝试序列化方法,并按预期工作(参见 EDIT_2)
会不会是 Reflection.Emit 问题 and/or 我的运行时程序集?
EDIT_2: Xml 序列化的预期结果:
<CONTRL>
<Syntaxkennung>UNOC</Syntaxkennung>
<Sintaxversion>3</Sintaxversion>
<AbsenderID>9904325000003</AbsenderID>
<AbsenderCodeUNB>500</AbsenderCodeUNB>
<EmpfängerID>9900080000007</EmpfängerID>
<EmpfängerCodeUNB>500</EmpfängerCodeUNB>
<Dokumentdatum>161007</Dokumentdatum>
<Dokumentzeit>1723</Dokumentzeit>
</CONTRL>
收到:
<CONTRL>
<Sintaxversion>3</Sintaxversion>
</CONTRL>
我最终在 FormatBase (Base class) 中实现了 IXmlSerializable
public void WriteXml(XmlWriter writer)
{
foreach (PropertyInfo property in GetType().GetProperties())
{
writer.WriteElementString(property.Name, property.GetValue(this)?.ToString());
}
}
并按预期工作。