简体   繁体   中英

Null part in deserialize xml to class object

I must deserialize some xml to object class which I generate from xsd files using xsd.exe. Everything is okey but one part in my object is always null and I don't know why because in xml it have data.

It's the xml file:

<?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>

And this is my class from xsd files:

[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;
        }
    }
}

I left only the most necessary part because full version i very long.

You need to set the XML namespace for the data property correctly by applying XmlElementAttribute :

    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;
        }
    }

Also, as Richard Schneider wrote, change the type of data to be T_data . If you leave it as an object property your even:data element tree will be deserialized as an XmlNode [] array which is probably not what you want.

(The easiest way to find and fix "XML property deserialized as null " bugs is to create an example of the class in memory, serialize to XML, and compare the output with your input XML. Usually you'll spot the difference; often it's an incorrect namespace.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM