将 xml 反序列化为 class 对象中的 Null 部分

Null part in deserialize xml to class object

我必须将一些 xml 反序列化为我使用 xsd.exe 从 xsd 文件生成的对象 class。一切都很好,但我的对象中的一部分总是空的,我不知道为什么,因为在 xml 中它有数据。

这是 xml 文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<jdf:root xmlns:jdf="http://www.tmp.com/jdf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <jdf:header>
        <jdf:trace-id>string</jdf:trace-id>
        <jdf:timestamp>string</jdf:timestamp>
        <jdf:command>string</jdf:command>
        <jdf:version>string</jdf:version>
    </jdf:header>
    <even:data xmlns:even="http://tmp.com/zzz/pivot/event">
        <even:event xmlns:com="http://tmp.com/zzz/utils/components">
            <even:eventId>3</even:eventId>
            <even:distributorId>string</even:distributorId>
            <even:distributionNetworkId>string</even:distributionNetworkId>
            <even:typology>string</even:typology>
        </even:event>
    </even:data>
</jdf:root>

这是我的 class 来自 xsd 个文件:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.tmp.com/jdf")]
[System.Xml.Serialization.XmlRootAttribute("root", Namespace = "http://www.tmp.com/jdf", IsNullable = false)]
public partial class JdfRoot
{

    private JdfHeader headerField;

    private object dataField;

    /// <uwagi/>
    public JdfHeader header
    {
        get
        {
            return this.headerField;
        }
        set
        {
            this.headerField = value;
        }
    }


    /// <uwagi/>
    public object data
    {
        get
        {
            return this.dataField;
        }
        set
        {
            this.dataField = value;
        }
    }
}

/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://mib.bnpp.com/cle/pivot/event")]
[System.Xml.Serialization.XmlRootAttribute("data", Namespace = "http://tmp.com/cle/pivot/event", IsNullable = false)]
public partial class T_data
{

    private EventOut eventField;

    /// <uwagi/>
    public EventOut @event
    {
        get
        {
            return this.eventField;
        }
        set
        {
            this.eventField = value;
        }
    }
}

我只留下了最必要的部分,因为完整版我很长。

JdfRoot 中,将 public object data 更改为 public T_data data

您需要通过应用 XmlElementAttribute:

data 属性 正确设置 XML 命名空间
    private T_data dataField;

    [XmlElement("data", Namespace = "http://tmp.com/zzz/pivot/event")]
    public T_data data
    {
        get
        {
            return this.dataField;
        }
        set
        {
            this.dataField = value;
        }
    }

此外,正如 Richard Schneider 所写,将 data 的类型更改为 T_data。如果您将其保留为 object 属性,您的 even:data 元素树将被反序列化为 XmlNode [] 数组,这可能不是您想要的。

(查找和修复"XML property deserialized as null" bug最简单的方法是在内存中创建一个class的例子,序列化到XML,并将输出与您的输入进行比较 XML。通常您会发现差异;通常是命名空间不正确。)