反序列化XML会产生错误消息=“<myclass xmlns =”http://medicareaustralia.gov.au/eclaiming/version 2“>不是预期的。”

时间:2015-11-18 07:39:50

标签: xml vb.net xmlserializer

我遇到了反序列化以下xml的问题。我得到的例外情况是“http://medicareaustralia.gov.au/eclaiming/version 2”&gt;不是预期的。“ 我缺乏对XML的接触以及我有限的理解,这让我成为了解决问题的噩梦。

这就是我的XML:

<?xml version="1.0" encoding="UTF-8" ?> 
<ns1:patienteClaimingResponse xmlns:ns1="http://medicareaustralia.gov.au/eclaiming/version  2">
    <claim medicareAcceptanceType="NAC" assessmentStatus="OK" accountPaidInd="N">
        <voucher voucherId="01">
            <service serviceId="0001" dateOfService="2015  11  17+11:00" assessmentStatus="ERR" chargeAmount="14815" patientContribAmt="4000" mbsItemNum="55036">
            <assessmentExplanation code="9632" /> 
            </service>
            <service serviceId="0002" dateOfService="2015 11 17+11:00" assessmentStatus="OK" chargeAmount="14605" benefitAmount="1" patientContribAmt="4500" scheduleFee="1" mbsItemNum="55032">
            <assessmentExplanation code="0" /> 
            </service>
        </voucher>
    <claimant currentSubnumerate="1" currentMedicareCardNum="6950323991">
        <identity firstName="CELINA" lastName="HOBBS" /> 
    </claimant>
    <patient currentSubnumerate="1" currentMedicareCardNum="6950323991">
        <identity firstName="CELINA" lastName="HOBBS" /> 
    </patient>
    <provider providerNum="2402491W">
        <name>L ARKIE</name> 
    </provider>
    <assessmentError code="0" /> 
    </claim>
</ns1:patienteClaimingResponse>

我想做的是将这个xml反序列化到我的类中,我的类定义如下所示:

Imports System.Xml.Serialization

<XmlRoot(ElementName:="patienteClaimingResponse", Namespace:="")>
Public Class PatienteClaimingResponse
    <XmlElement("claim")>
    Public Property Claim As Claim
End Class

Public Class Claim
    <XmlAttributeAttribute("medicareAcceptanceType")>
    Public Property MedicareAcceptanceType As String

    <XmlAttributeAttribute("assessmentStatus")>
    Public Property AssessmentStatus As String

    <XmlAttributeAttribute("accountPaidInd")>
    Public Property AccountPaidInd As String

    <XmlElement("voucher")>
    Public Property Voucher As Voucher

    <XmlElement("claimant")>
    Public Property Claimant As Claimant

    <XmlElement("patient")>
    Public Property Patient As Patient

    <XmlElement("provider")>
    Public Property Provider As Provider

    <XmlElement("assessmentError")>
    Public Property AssessmentError As AssessmentError

End Class

Public Class Voucher
    <XmlAttributeAttribute("voucherId")>
    Public Property VoucherId As String

    <XmlElement("service")>
    Public Property Services As List(Of Service)
End Class

Public Class Service
    <XmlAttributeAttribute("serviceId")>
    Public Property ServiceId As String

    <XmlAttributeAttribute("dateOfService")>
    Public Property DateOfService As String

    <XmlAttributeAttribute("assessmentStatus")>
    Public Property AssessmentStatus As String

    <XmlAttributeAttribute("chargeAmount")>
    Public Property ChargeAmount As String

    <XmlAttributeAttribute("benefitAmount")>
    Public Property BenefitAmount As String

    <XmlAttributeAttribute("patientContribAmt")>
    Public Property PatientContribAmt As String

    <XmlAttributeAttribute("scheduleFee")>
    Public Property ScheduleFee As String

    <XmlAttributeAttribute("mbsItemNum")>
    Public Property MbsItemNum As String

    <XmlElement("assessmentExplanation")>
    Public Property assessmentExplanation As AssessmentError

End Class

Public Class Claimant
    <XmlAttributeAttribute("CurrentSubnumerate")>
    Public Property currentSubnumerate As String

    <XmlAttributeAttribute("CurrentMedicareCardNum")>
    Public Property currentMedicareCardNum As String

    <XmlElement("identity")>
    Public Property Identity As Identity
End Class

Public Class Patient
    <XmlAttributeAttribute("CurrentSubnumerate")>
    Public Property currentSubnumerate As String

    <XmlAttributeAttribute("CurrentMedicareCardNum")>
    Public Property currentMedicareCardNum As String

    <XmlElement("identity")>
    Public Property Identity As Identity
End Class

Public Class Provider
    <XmlAttributeAttribute("providerNum")>
    Public Property ProviderNum As String

    <XmlElement("name")>
    Public Property Name As String
End Class

Public Class Identity
    <XmlAttributeAttribute("firstName")>
    Public Property FirstName As String

    <XmlAttributeAttribute("lastName")>
    Public Property LastName As String
End Class

Public Class AssessmentError
    <XmlAttributeAttribute("code")>
    Public Property Code As String
End Class

这是我的反序列化代码

Dim ppClaim As New PatienteClaimingResponse
Dim serializer As New Xml.Serialization.XmlSerializer(ppClaim.GetType)
Try
     Dim mystreamReader As New System.IO.StreamReader("C:\Laavanya\EasyClaimRes.xml")
     Dim TargetObject As New PatienteClaimingResponse
     TargetObject = serializer.Deserialize(mystreamReader)
     mystreamReader.Close()
Catch ex As Exception
     Dim s As String = ex.InnerException.ToString 
     'Throws "<patienteClaimingResponse xmlns='http://medicareaustralia.gov.au/eclaiming/version  2'> was not expected."
End Try

我注意到的是,如果我删除命名空间的别名,即ns1,deserialise似乎没有抱怨。如果是这种情况,有没有办法用别名信息来装饰我的类?

1 个答案:

答案 0 :(得分:0)

您的问题是根元素 iteself 位于命名空间"http://medicareaustralia.gov.au/eclaiming/version 2"中,但所有子元素都不在命名空间中。因此,您需要这样做:

<XmlRoot(ElementName:="patienteClaimingResponse", Namespace:="http://medicareaustralia.gov.au/eclaiming/version  2")>
Public Class PatienteClaimingResponse
    <XmlElement("claim", Namespace:="")>
    Public Property Claim As Claim
End Class

工作fiddle