簡體   English   中英

將xml反序列化為類對象的無效部分

[英]Null part in deserialize xml to class object

我必須反序列化一些xml到使用xsd.exe從xsd文件生成的對象類。 一切都很好,但對象中的一部分始終為空,我也不知道為什么,因為在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>

這是我來自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;
        }
    }
}

我只剩下了最必要的部分,因為完整版非常長。

您需要通過應用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 []數組,這可能不是您想要的。

(查找和修復“反序列化為null XML屬性”錯誤的最簡單方法是在內存中創建該類的示例, 序列化為 XML,然后將輸出與輸入XML進行比較。通常,您會發現其中的區別;通常是不正確的名稱空間。)

JdfRoot ,將public object data更改為public T_data data

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM