属性未被序列化

时间:2017-03-20 15:03:47

标签: c# xml-serialization xmlserializer reflection.emit

我使用reflection.emit生成一个运行时类,该类的生成似乎很好,因为它在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(唯一一个Type 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: 在进一步测试后,我复制粘贴生成的类并将其创建为" normal"键入我的项目,再次尝试序列化方法,并按预期工作(请参阅EDIT_2)

可以是Reflection.Emit问题和/或我的运行时程序集吗?

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>

1 个答案:

答案 0 :(得分:0)

我最终在FormatBase(Base class)中实现了IXmlSerializable

Tue Dec  3 00:00:00 2019

按预期工作。