Ich versuche ein XML-Dokument zu deserialisieren, aber der Code, den ich verwende, gibt jedes Mal den Nullwert zurück.Das Deserialisieren von XML in ein Objekt gibt Nullwerte zurück
Ich habe eine XML-ähnliche
<?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>
es zu deserialisieren, ich habe eine Klasse erstellt
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;
}
}
und den Code ich verwende ist
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);
}
}
Hier ist gibt immer den Nullwert zurück. Vielen Dank im Voraus für Ihre Hilfe.
Erscheint eine Ausnahme, wenn Sie debuggen? –
Nein, ich habe versucht, den Wert von RegistrationID in TextBox zu erhalten, und es druckt 0 – Hanumendra
Welchen Code befindet sich in Ihrer 'InfoClass'? Gibt es irgendwelche XML-Attribute? –