无法在vb.net中为xsd.exe创建的类转换类型错误的对象

时间:2015-01-28 12:53:26

标签: xml vb.net oop xml-serialization xsd.exe

我使用xsd.exe创建了一个类" AnnualReturnA"来自这个架构:

http://xmlgw.companieshouse.gov.uk/v1-0/schema/forms/AnnualReturn-v2-6.xsd

我正在努力实现这个类。我已将我的测试程序和相关的片段放在下面。

我的问题是我在线路上执行测试程序时遇到错误oAR.Officer(0)= AROfficer.Item(0)错误"无法转换类型' DirectorPersonType&#39的对象;输入' AnnualReturnOfficer'"。

有人可以提供有关如何解决此错误的任何建议吗?我假设我在我的程序中错误地实施了课程,但我不知道该怎么做。这是我第一次使用自定义类...

测试程序:

Sub Main()

    Dim oAR As New AnnualReturnA.AnnualReturn
    Dim Add As New AnnualReturnA.UKAddressType
    Dim Add2 As New AnnualReturnA.UKAddressType
    Dim AROfficer As New AnnualReturnA.AnnualReturnOfficer
    Dim DirRetType As New AnnualReturnA.DirectorReturnType
    Dim DirPerType As New AnnualReturnA.DirectorPersonType

    oAR.CompanyCategory = AnnualReturnA.CompanyCategory.BYSHR
    oAR.MadeUpDate = #1/1/1999#
    ReDim oAR.Items(1)
    ReDim oAR.ItemsElementName(1)

    oAR.ItemsElementName(0) = AnnualReturnA.ItemsChoiceType.TradingOnMarket
    oAR.ItemsElementName(1) = AnnualReturnA.ItemsChoiceType.DTR5Applies

    oAR.Items(0) = True
    oAR.Items(1) = False

    ReDim oAR.SICCodes(1)

    oAR.SICCodes(0) = "4672"
    oAR.SICCodes(1) = "4783"

    Add.Postcode = "asdf"
    Add.CareofName = "Care Of"
    Add.Street = "strret"

    oAR.RegisteredOfficeAddress = Add

    Add2.Postcode = "sailasdf"
    Add2.CareofName = "sailCare Of"
    Add2.Street = "sailstrret"

    DirPerType.Forename = "Test Forename"
    DirPerType.Occupation = "Accountant"

    ReDim DirRetType.Item(1)

    DirRetType.Item(0) = DirPerType

    ReDim oAR.Officer(3)
    ReDim AROfficer.Item(3)

    AROfficer.Item(0) = DirRetType.Item(0)

    oAR.Officer(0) = AROfficer.Item(0)

    Dim xS As New XmlSerializer(oAR.GetType)
    Dim objStreamWriter As New StreamWriter("E:\testSerial.xml")
    xS.Serialize(objStreamWriter, oAR)
    objStreamWriter.Close()

End Sub

官:

    <System.Xml.Serialization.XmlElementAttribute("Officer")> _
    Public Property Officer() As AnnualReturnOfficer()
        Get
            Return Me.officerField
        End Get
        Set(value As AnnualReturnOfficer())
            Me.officerField = value
        End Set
    End Property  

年度回归主任:

    Partial Public Class AnnualReturnOfficer

    Private itemField As Object

    <System.Xml.Serialization.XmlElementAttribute("Director", GetType(DirectorReturnType)), _
     System.Xml.Serialization.XmlElementAttribute("Member", GetType(MemberReturnType)), _
     System.Xml.Serialization.XmlElementAttribute("Secretary", GetType(SecretaryReturnType))> _
    Public Property Item() As Object
        Get
            Return Me.itemField
        End Get
        Set(value As Object)
            Me.itemField = value
        End Set
    End Property
End Class

导演回复类型:

Partial Public Class DirectorReturnType

    Private itemField As Object

    <System.Xml.Serialization.XmlElementAttribute("Corporate", GetType(CorporateOfficerType)), _
     System.Xml.Serialization.XmlElementAttribute("Person", GetType(DirectorPersonType))> _
    Public Property Item() As Object
        Get
            Return Me.itemField
        End Get
        Set(value As Object)
            Me.itemField = value
        End Set
    End Property
End Class

导演人员类型:

Partial Public Class DirectorPersonType
    Inherits PersonBaseType

    Private serviceAddressField As ServiceAddressType

    Private dOBField As Date

    Private nationalityField As String

    Private occupationField As String

    Private countryOfResidenceField As String

    Private previousNamesField() As PreviousNameType

    Public Property ServiceAddress() As ServiceAddressType
        Get
            Return Me.serviceAddressField
        End Get
        Set(value As ServiceAddressType)
            Me.serviceAddressField = value
        End Set
    End Property

0 个答案:

没有答案