none
XmlSerializer.Deserialize RRS feed

  • Question

  • Using Visual Studio 2015 and .NET framework 4.6 I created a class using the Edit->Paste Special->Past XML as classes feature to create the classes needed to parse the XML I was needing to read.

    When I try to Deserialize the XML to the class created I get the error 

    System.InvalidOperationException: There is an error in XML document (3, 2). ---> System.InvalidOperationException: <ClinicalDocument xmlns='urn:hl7-org:v3'> was not expected.

    The XML looks like...

    <?xml version="1.0"?>
    <?xml-stylesheet type='text/xsl' href='CDA.XSL'?>
    <ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" moodCode="EVN" xmlns="urn:hl7-org:v3">
     <realmCode code="US" />
     <typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040" />
     <templateId root="2.16.840.1.113883.10.20.22.1.1" />
     <templateId root="2.16.840.1.113883.10.20.22.1.2" />
    .......

    The class is declared as 

    <System.SerializableAttribute(),
     System.ComponentModel.DesignerCategoryAttribute("code"),
     System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="urn:hl7-org:v3"),
     System.Xml.Serialization.XmlRootAttribute([Namespace]:="urn:hl7-org:v3", IsNullable:=False)>
    Partial Public Class ClinicalDocument
        Private realmCodeField As ClinicalDocumentRealmCode
        Private typeIdField As ClinicalDocumentTypeId
        Private templateIdField() As ClinicalDocumentTemplateId
        Private idField As ClinicalDocumentID

    Finally its deserialized like..

            Dim xmlCDA As New System.Xml.Serialization.XmlSerializer(GetType(clsCCDA), "urn:hl7-org:v3")
            Dim srFile As New IO.StreamReader("F:\Temp\DOC00001.XML")
            Dim objCDA As clsCCDA
            Try
                '==objCDA = New clsCCDA
                objCDA = xmlCDA.Deserialize(srFile)
            Catch ex As Exception
                Dim serror As String = ex.ToString
                MsgBox(ex.ToString)
                Console.WriteLine(ex.ToString)
            Finally
                srFile.Close()
                srFile.Dispose()
            End Try

    It does not matter if I include the namespace in the new serializer or not, the error is the same. 

    I have seen posts all over the internet but no solutions. Anyone been able to fix this?


    Lee

    Thursday, July 6, 2017 9:41 PM

Answers

  • Try specifying the right class instead of clsCCDA:

       Dim xmlCDA As New XmlSerializer(GetType(ClinicalDocument), "urn:hl7-org:v3")

       Dim objCDA As ClinicalDocument
       . . .

    • Marked as answer by Lee Taylor Friday, July 7, 2017 12:23 PM
    Friday, July 7, 2017 5:28 AM

All replies

  • Try specifying the right class instead of clsCCDA:

       Dim xmlCDA As New XmlSerializer(GetType(ClinicalDocument), "urn:hl7-org:v3")

       Dim objCDA As ClinicalDocument
       . . .

    • Marked as answer by Lee Taylor Friday, July 7, 2017 12:23 PM
    Friday, July 7, 2017 5:28 AM
  • So obvious, I guess I stared at it too long.

    Lee

    Friday, July 7, 2017 12:23 PM