将XML反序列化为对象将返回空值

时间:2016-06-11 07:45:33

标签: c# xml winforms deserialization

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

1 个答案:

答案 0 :(得分:1)

您的问题出在xml中,因为它有一个注册列表。如果您删除了(function () { "use strict"; var app = angular.module("myApp", ["common.services", "ui.router", "ui.mask", "userService", "ui.bootstrap"]); app.config(["$stateProvider", "$urlRouterProvider", function ($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise("/"); $stateProvider .state("index", { url: "/", templateUrl: "app/index.html", }) // Restricted .state("home", { url: "/home", templateUrl: "app/home/home.html", controller: "HomeController as vm", data: { requiresLogin: true } }) }] ).run(['$rootScope', '$state', 'userService', function($rootScope, $state, userService) { $rootScope.$on('$stateChangeStart', function(e, to) { if (to.data && to.data.requiresLogin && !userService.isLogged) { e.preventDefault(); $state.go('index'); } }) }]); }()); <Registration>代码,那么它就有效。您需要<Registrations>Registration,因为在这种情况下您必须使用Registrations

你可以像这个例子(Deserializing nested xml into C# objects

那样做

并创建一个自己的班级Lists,其中包含RegistrationsList元素。

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

Registration

[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