Trouble deserializing xml nodes

Russ1984

Member
Joined
Feb 29, 2008
Messages
9
Programming Experience
Beginner
Hi,

I have an xml document that for my purposes here can be shortened to:

HTML:
<?xml version="1.0" encoding="UTF-8"?>
<Report
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="DCLG-CEPC"
	xsi:schemaLocation="DCLG-CEPC ..\Reports\CEPC-Reports.xsd"
>
	<Report-Header>
		<RRN>0455-3619-8688-5206-9225</RRN>
		<Issue-Date>2008-01-28</Issue-Date>
		<Report-Type>3</Report-Type>
		<Valid-Until>2018-01-27</Valid-Until>
		<Related-RRN>6946-8248-5680-5129-2506</Related-RRN>
		<Inspection-Date>2008-01-21</Inspection-Date>
		<Registration-Date>2008-01-28</Registration-Date>
		<Status>entered</Status>
		<Language-Code>1</Language-Code>
		<Energy-Assessor>
			<Certificate-Number>DFGT525256</Certificate-Number>
			<Name>John Smith</Name>
			<Accreditation-Scheme-Name>ABC Accreditation Ltd</Accreditation-Scheme-Name>
			<Trading-Address>Alpha House, New Way, Birmingham, B2 1AA</Trading-Address>
			<Company-Name>EnergyWatch Ltd</Company-Name>
			<Related-Party-Disclosure> </Related-Party-Disclosure>
			<Qualifications>NOS3</Qualifications>
			<Status>Registered</Status>
		</Energy-Assessor>
		<Property-Details>
			<Location-Description></Location-Description>
			<UPRN>458622158656</UPRN>
			<Property-Address>
				<Address-Line-1>Example building</Address-Line-1>
				<Address-Line-2>56 London Road</Address-Line-2>
				<Post-Town>LONDON</Post-Town>
				<Postcode>SW23 1HA</Postcode>
			</Property-Address>
			<Property-Type>OFFICE</Property-Type>
			<Occupier>John Jones</Occupier>
			<Is-Heritage-Site>N</Is-Heritage-Site>
		</Property-Details>
		<Calculation-Details>
			<Methodology>iSBEM v3.0.a</Methodology>
			<Calculation-Tool>SBEM v3.0.a_dev (JAN08)</Calculation-Tool>
			<Inspection-Type>Physical</Inspection-Type>
		</Calculation-Details>
	</Report-Header>
	<Insurance-Details>
		<Insurer>My company for insurance</Insurer>
		<Policy-No>0123456789</Policy-No>
		<Effective-Date>2007-12-07</Effective-Date>
		<Expiry-Date>2011-12-09</Expiry-Date>
		<PI-Limit>500000</PI-Limit>
	</Insurance-Details>
	<Energy-Performance-Certificate>
		<EPC-Rating>
			<Asset-Rating>57</Asset-Rating>
			<New-Build-Benchmark>57</New-Build-Benchmark>
			<Existing-Stock-Benchmark>106</Existing-Stock-Benchmark>
		</EPC-Rating>
		<EPC-Rating-Scale>
			<Upper-Number-A-Scale>24</Upper-Number-A-Scale>
			<Lower-Number-B-Scale>25</Lower-Number-B-Scale>
			<Upper-Number-B-Scale>50</Upper-Number-B-Scale>
			<Lower-Number-C-Scale>51</Lower-Number-C-Scale>
			<Upper-Number-C-Scale>77</Upper-Number-C-Scale>
			<Lower-Number-D-Scale>78</Lower-Number-D-Scale>
			<Upper-Number-D-Scale>106</Upper-Number-D-Scale>
			<Lower-Number-E-Scale>107</Lower-Number-E-Scale>
			<Upper-Number-E-Scale>136</Upper-Number-E-Scale>
			<Lower-Number-F-Scale>137</Lower-Number-F-Scale>
			<Upper-Number-F-Scale>167</Upper-Number-F-Scale>
			<Lowest-Number-G-Scale>167</Lowest-Number-G-Scale>
		</EPC-Rating-Scale>
		<Technical-Information>
			<Main-Heating-Fuel>Grid supplied electricity</Main-Heating-Fuel>
			<Building-Environment>Air Conditioning</Building-Environment>
			<Floor-Area>2900</Floor-Area>
			<Building-Level>3</Building-Level>
		</Technical-Information>
	</Energy-Performance-Certificate>
</Report>
I have been able to deserialize the nodes 'Report-Header' and 'Insurance-Details' fine with this code below for 'Report-Header':

VB.NET:
        ' Returns a ReportHeader object filled values from the specified xml document
        Public Function readReportHeader(ByVal filename As String) As CPEC_Reports.ComClass.ReportHeader
            Dim d As New XmlDocument()
            Dim s As New XmlSerializer(GetType(CPEC_Reports.ComClass.ReportHeader))
            Dim nl As XmlNodeList
            d.Load(filename)
            nl = d.GetElementsByTagName("Report-Header")
            Dim nr As New XmlNodeReader(nl(0))
            Return s.Deserialize(nr)
        End Function
Which is great. However, when using identicle code (except for the name and type of the object) when I deserialize an 'Energy-Performance-Certificate' or even an 'EPC-Rating' I get all zero values.

Does anyone have any ideas as to what might be going wrong??

Thanks for any help in advance.
Russ.
 
Hazar!

Over that hurdle, brilliant! The problem seemed to be in the attributes the object class itself. For the 'EPC-Rating' (eg) I had this

HTML:
<System.Xml.Serialization.XmlElementAttribute("Asset-Rating"), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified> _
        Public Property AssetRating() As Integer
            Get
                Return Me.assetRatingField
            End Get
            Set(ByVal value As Integer)
                Me.assetRatingField = value
            End Set
        End Property
and removing the 'Form:=System....' part gave me the values I was after.
However, I have very quickly run into another problem. When trying to deserialize a class called 'TechnicalInformation' in the same way I am greeted with the msg 'There is an error in the XML document'. Maybe this line is something about requirement because not all of the fields in the 'TechnicalInformation' class are specified in the xml what I really need is for it to fill the ones that are there. The are boolean markers to say whether a field is specified with the class as seen above. But I'm a noob and don't really know what I'm doing.

Can anyone help?
 
I forgot to include the code for the 'TechnicalInformation' class and it's associated sub-classes. Here it is:

HTML:
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
     System.SerializableAttribute(), _
     System.Diagnostics.DebuggerStepThroughAttribute(), _
     System.ComponentModel.DesignerCategoryAttribute("code"), _
     System.Xml.Serialization.XmlTypeAttribute(TypeName:="Technical-Information", [Namespace]:="DCLG-CEPC"), _
     System.Xml.Serialization.XmlRootAttribute("Technical-Information", [Namespace]:="DCLG-CEPC", IsNullable:=False)> _
    <ComClass()> _
    Partial Public Class TechnicalInformation

        Private mainHeatingFuelField As HeatingFuelTypeCode

        Private mainHeatingFuelFieldSpecified As Boolean

        Private otherFuelDescriptionField As String

        Private buildingEnvironmentField As BuildingEnvironmentCode

        Private floorAreaField As Decimal

        Private buildingLevelField As Integer

        Private buildingLevelFieldSpecified As Boolean

        Private specialEnergyUsesField As String

        Private renewableSourcesField As String

        Private oRAvailabilityDateField As Date

        Private oRAvailabilityDateFieldSpecified As Boolean

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Main-Heating-Fuel")> _
        Public Property MainHeatingFuel() As HeatingFuelTypeCode
            Get
                Return Me.mainHeatingFuelField
            End Get
            Set(ByVal value As HeatingFuelTypeCode)
                Me.mainHeatingFuelField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlIgnoreAttribute()> _
        Public Property MainHeatingFuelSpecified() As Boolean
            Get
                Return Me.mainHeatingFuelFieldSpecified
            End Get
            Set(ByVal value As Boolean)
                Me.mainHeatingFuelFieldSpecified = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Other-Fuel-Description")> _
        Public Property OtherFuelDescription() As String
            Get
                Return Me.otherFuelDescriptionField
            End Get
            Set(ByVal value As String)
                Me.otherFuelDescriptionField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Building-Environment")> _
        Public Property BuildingEnvironment() As BuildingEnvironmentCode
            Get
                Return Me.buildingEnvironmentField
            End Get
            Set(ByVal value As BuildingEnvironmentCode)
                Me.buildingEnvironmentField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Floor-Area")> _
        Public Property FloorArea() As Decimal
            Get
                Return Me.floorAreaField
            End Get
            Set(ByVal value As Decimal)
                Me.floorAreaField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Building-Level")> _
        Public Property BuildingLevel() As Integer
            Get
                Return Me.buildingLevelField
            End Get
            Set(ByVal value As Integer)
                Me.buildingLevelField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlIgnoreAttribute()> _
        Public Property BuildingLevelSpecified() As Boolean
            Get
                Return Me.buildingLevelFieldSpecified
            End Get
            Set(ByVal value As Boolean)
                Me.buildingLevelFieldSpecified = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Special-Energy-Uses")> _
        Public Property SpecialEnergyUses() As String
            Get
                Return Me.specialEnergyUsesField
            End Get
            Set(ByVal value As String)
                Me.specialEnergyUsesField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("Renewable-Sources")> _
        Public Property RenewableSources() As String
            Get
                Return Me.renewableSourcesField
            End Get
            Set(ByVal value As String)
                Me.renewableSourcesField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlElementAttribute("OR-Availability-Date")> _
        Public Property ORAvailabilityDate() As Date
            Get
                Return Me.oRAvailabilityDateField
            End Get
            Set(ByVal value As Date)
                Me.oRAvailabilityDateField = value
            End Set
        End Property

        '''<remarks/>
        <System.Xml.Serialization.XmlIgnoreAttribute()> _
        Public Property ORAvailabilityDateSpecified() As Boolean
            Get
                Return Me.oRAvailabilityDateFieldSpecified
            End Get
            Set(ByVal value As Boolean)
                Me.oRAvailabilityDateFieldSpecified = value
            End Set
        End Property
    End Class

    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
     System.SerializableAttribute(), _
     System.Xml.Serialization.XmlTypeAttribute([Namespace]:="DCLG-CEPC")> _
    Public Enum HeatingFuelTypeCode

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Natural Gas")> _
        NaturalGas

        '''<remarks/>
        LPG

        '''<remarks/>
        Biogas

        '''<remarks/>
        Oil

        '''<remarks/>
        Coal

        '''<remarks/>
        Anthracite

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Smokeless Fuel (inc Coke)")> _
        SmokelessFuelincCoke

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Dual Fuel Appliances (Mineral + Wood)")> _
        DualFuelAppliancesMineralWood

        '''<remarks/>
        Biomass

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Grid Supplied Electricity")> _
        GridSuppliedElectricity

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Grid Displaced Electricity")> _
        GridDisplacedElectricity

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Waste Heat")> _
        WasteHeat

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("District Cooling")> _
        DistrictCooling

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("District Heating")> _
        DistrictHeating

        '''<remarks/>
        Other
    End Enum

    '''<remarks/>
    <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
     System.SerializableAttribute(), _
     System.Xml.Serialization.XmlTypeAttribute([Namespace]:="DCLG-CEPC")> _
    Public Enum BuildingEnvironmentCode

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Air Conditioning")> _
        AirConditioning

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Heating and Natural Ventilation")> _
        HeatingandNaturalVentilation

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Heating and Mechanical Ventilation")> _
        HeatingandMechanicalVentilation

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Mixed-mode with Natural Ventilation")> _
        MixedmodewithNaturalVentilation

        '''<remarks/>
        <System.Xml.Serialization.XmlEnumAttribute("Mixed-mode with Mechanical Ventilation")> _
        MixedmodewithMechanicalVentilation
    End Enum
 
Back
Top