2016-06-11 12 views
0

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.

+0

Erscheint eine Ausnahme, wenn Sie debuggen? –

+0

Nein, ich habe versucht, den Wert von RegistrationID in TextBox zu erhalten, und es druckt 0 – Hanumendra

+0

Welchen Code befindet sich in Ihrer 'InfoClass'? Gibt es irgendwelche XML-Attribute? –

Antwort

1

Ihr Problem ist in der XML, weil es eine Liste von Registrierungen hat. Wenn Sie <Registration> und <Registrations> Tag entfernen, funktioniert es. Benötigen Sie die Registration und Registrations, weil Sie in diesem Fall mit Lists arbeiten müssen.

Man könnte es wie in diesem Beispiel tun (Deserializing nested xml into C# objects)

Und eine eigene Klasse erstellen Registrations die eine List von Registration Elements halten.

Mit diesem Code funktioniert es. Erstellen Sie eine Superklasse:

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

und die Registrations:

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

und die Registration sollte die gleiche wie zuvor. Die Hauptfunktion sollte sich dies ändern:

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); 
     } 
} 
+0

Ich kann diese Tags nicht manuell entfernen, soll ich diese durch Code entfernen? – Hanumendra

+0

Sie können es tun, wie ich in der Antwort beschrieben habe, indem Sie eine "Registrations" -Klasse verwenden oder wie Sie erwähnten, diese Tags zu entfernen. –

+0

@Hanumendra funktioniert die Lösung? –

Verwandte Themen