簡體   English   中英

將 XML 反序列化為對象返回空值

[英]Deserializing XML into object returns null values

我正在嘗試反序列化 XML 文檔,但我使用的代碼每次都返回 Null 值。

我有一個這樣的 XML

<?xml version="1.0" encoding="utf-8"?>
<RegistrationOpenData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.gov">
<Description>Registration data is collected by ABC XYZ</Description>
<InformationURL>http://www.example.com/html/hpd/property-reg-unit.shtml</InformationURL>
<SourceAgency>ABC Department of Housing</SourceAgency>
<SourceSystem>PREMISYS</SourceSystem>
<StartDate>2016-02-29T00:03:06.642772-05:00</StartDate>
<EndDate i:nil="true" />
<Registrations>
<Registration xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<RegistrationID>108260</RegistrationID>
<BuildingID>4731</BuildingID>
</Registration>
</Registrations>
</RegistrationOpenData>

為了反序列化它,我創建了一個類

using System.Xml.Serialization;
using System.Xml;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.gov")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://example.gov", IsNullable=true)]
public partial class Registration : InfoClass {

  private long registrationIDField;
  private bool registrationIDFieldSpecified;
  private System.Nullable<long> buildingIDField;
  private bool buildingIDFieldSpecified;

  public long RegistrationID
 {
    get
    {
        return this.registrationIDField;
    }
    set
    {
        this.registrationIDField = value;
    }
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool RegistrationIDSpecified {
    get {
        return this.registrationIDFieldSpecified;
    }
    set {
        this.registrationIDFieldSpecified = value;
    }
}

[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<long> BuildingID {
    get {
        return this.buildingIDField;
    }
    set {
        this.buildingIDField = value;
    }
}

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool BuildingIDSpecified {
    get {
        return this.buildingIDFieldSpecified;
    }
    set {
        this.buildingIDFieldSpecified = value;
    }
}

我使用的代碼是

public void Test()
    {

        Registration RegistrationVal = null;
        var xRoot = new XmlRootAttribute();
        xRoot.ElementName = "RegistrationOpenData";
        xRoot.Namespace = "http://services.hpd.gov";
        xRoot.IsNullable = true;
        var serializer = new XmlSerializer(typeof(Registration), xRoot);
        using (TextReader reader = new StreamReader(@"D:\sample.xml"))
            {
                RegistrationVal = (Registration)serializer.Deserialize(reader);
            }
}

這里它總是返回 Null 值。 預先感謝您的幫助。

您的問題出在 xml 中,因為它有一個注冊列表。 如果您刪除<Registration><Registrations>標記,則它可以工作。 您是否需要RegistrationRegistrations因為在這種情況下您必須使用Lists

你可以像在這個例子中那樣做(將嵌套的 xml 反序列化為 C# 對象

並創建一個自己的類Registrations ,其中包含Registration元素List

使用此代碼它可以工作。 創建一個超類:

[XmlRoot("RegistrationOpenData")]
public class RegistrationOpenData
{
    [XmlElement("Registrations")]
    public Registrations Regs { get; set; }
}

Registrations

[XmlRoot("Registrations")]
public class Registrations
{
    [XmlElement("Registration")]
    public List<Registration> Regs { get; set; }
}

並且Registration應該和以前一樣。 主要功能應更改為:

static void Main(string[] args)
{
        RegistrationOpenData RegistrationVal = null;
        var xRoot = new XmlRootAttribute();
        xRoot.ElementName = "RegistrationOpenData";
        xRoot.Namespace = "http://services.hpd.gov";
        xRoot.IsNullable = true;
        var serializer = new XmlSerializer(typeof(RegistrationOpenData), xRoot);
        using (TextReader reader = new StreamReader(@"D:\sample.xml"))
        {
            RegistrationVal = (RegistrationOpenData)serializer.Deserialize(reader);
        }
}

暫無
暫無

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

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