【问题标题】:Deserializing XML into object returns null values将 XML 反序列化为对象返回空值
【发布时间】:2016-06-11 07:45:33
【问题描述】:

我正在尝试反序列化 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 值。 提前感谢您的帮助。

【问题讨论】:

  • 调试时是否出现异常?
  • 不,我试图在 TextBox 中获取 RegistrationID 的值,结果显示为 0
  • 您的InfoClass 中有什么代码?有没有xml属性?
  • 没有 XMLElements

标签: c# xml winforms deserialization


【解决方案1】:

您的问题出在 xml 中,因为它有一个注册列表。如果您删除 &lt;Registration&gt;&lt;Registrations&gt; 标签,那么它可以工作。您是否需要RegistrationRegistrations,因为在这种情况下您必须使用Lists

你可以像这个例子中那样做 (Deserializing nested xml into C# objects)

并创建一个自己的类Registrations,其中包含ListRegistration 元素。

使用此代码可以正常工作。创建一个超类:

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

【讨论】:

  • 我无法手动删除这些标签,我应该通过代码删除它们吗?
  • 您可以像我在答案中描述的那样使用Registrations 类或像您提到的那样删除此标签。
  • 感谢您的帮助凯文,我觉得您的解决方案会奏效,我会在一段时间内应用它。现在 StreamReader 没有返回任何值。我已经厌倦了使用 XMLTextReader 和 StreamReader,实际上我正在解析的文件是 560MB,现在这也是一个大问题
  • @Hanumendra 我尝试了上面的代码,它在我的电脑上运行。您可以关闭此问题,当StreamReader 的问题没有消失时,您可以打开一个新问题:)
  • 刚刚标记了您的答案 :) 非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多