3

I am trying to deserialize an XML document but the code I using is returning Null Value each time.

I have a XML like this

<?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>

to deserialize it, I have created a class

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

and the code I am using is

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

Here it is always returning Null value. Thanks in advance for your help.

Hanumendra
  • 309
  • 2
  • 9
  • 22

1 Answers1

1

Your problem is in the xml because it has a list of registrations. If you remove <Registration> and <Registrations> tag then it works. Do you need the Registration and Registrations because in this case you have to work with Lists.

You could do it like in this example (Deserializing nested xml into C# objects)

And create a own class Registrations which hold a List of Registration Elements.

With this code it works. Create a super class:

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

and the Registrations:

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

and the Registration should be the same as before. The main function should change to this:

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);
        }
}
Community
  • 1
  • 1
wake-0
  • 3,918
  • 5
  • 28
  • 45
  • I cannot remove those tags manually, shall I remove those through code? – Hanumendra Jun 11 '16 at 08:26
  • You can do it like i described in the answer by using a `Registrations` class or like you mentioned removing this tags. – wake-0 Jun 11 '16 at 08:34
  • Thanks for your help Kevin, I feel your solution will work, I will apply this in a while. Right now StreamReader not returning any value. I am tired of using XMLTextReader and StreamReader, actually the file I am parsing is 560MB and that is also a big problem now – Hanumendra Jun 11 '16 at 09:34
  • @Hanumendra i tried the above code and it worked on my computer. You can close this question and when the problem with the `StreamReader` does not dissapear you can open a new question :) – wake-0 Jun 11 '16 at 09:41
  • just marked your answer :) Really thanks for your help – Hanumendra Jun 11 '16 at 09:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114423/discussion-between-hanumendra-and-kevin-wallis). – Hanumendra Jun 11 '16 at 14:48