c# - Deserializing XML into object returns null values -
i trying deserialize xml document code using returning null value each time.
i have xml 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 collected 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, have created 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 { { return this.registrationidfield; } set { this.registrationidfield = value; } } [system.xml.serialization.xmlignoreattribute()] public bool registrationidspecified { { return this.registrationidfieldspecified; } set { this.registrationidfieldspecified = value; } } [system.xml.serialization.xmlelementattribute(isnullable=true)] public system.nullable<long> buildingid { { return this.buildingidfield; } set { this.buildingidfield = value; } } [system.xml.serialization.xmlignoreattribute()] public bool buildingidspecified { { return this.buildingidfieldspecified; } set { this.buildingidfieldspecified = value; } }
and code 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 returning null value. in advance help.
your problem in xml because has list of registrations. if remove <registration>
, <registrations>
tag works. need registration
, registrations
because in case have work lists
.
you in example (deserializing nested xml c# objects)
and create own class registrations
hold list
of registration
elements.
with code works. create super class:
[xmlroot("registrationopendata")] public class registrationopendata { [xmlelement("registrations")] public registrations regs { get; set; } }
and registrations
:
[xmlroot("registrations")] public class registrations { [xmlelement("registration")] public list<registration> regs { get; set; } }
and registration
should same before. main function should change 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); } }
Comments
Post a Comment