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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With