MohawkMEDIC / everest

The Everest Framework is designed to ease the creation, formatting, and transmission of HL7v3 and CDA structures with remote systems.
Apache License 2.0
38 stars 22 forks source link

Class that inherits from ClinicalDocument throws IndexOutOfRangeException #9

Closed MikeCheel closed 6 years ago

MikeCheel commented 6 years ago

If I create an empty class that inherits from ClinicalDocument, map some data into it and call this extension method on it I get an IndexOutOfRangeException:

    public static void ToStream(this ClinicalDocument clinicalDocument, Stream outputStream)
    {
        XmlWriter xw = XmlWriter.Create(outputStream, new XmlWriterSettings() { Indent = true });
        XmlStateWriter xsw = new XmlStateWriter(xw);
        XmlIts1Formatter xf = new XmlIts1Formatter();
        IFormatterGraphResult result = xf.Graph(xsw, clinicalDocument);
        xsw.Flush();            
    }

If I do the same mapping to a direct instance of ClinicalDocument itself it works. Why does it throw the exception on classes that inherit from ClinicalDocument?

For example:

public class TestDocument : ClinicalDocument
{}
justin-fyfe commented 6 years ago

I believe the issue is the class is missing the [Structure] attribute. This is what the formatter uses to determine how to serialize and de-serialize the request. There is a sample of how to use inheritance here : https://github.com/MohawkMEDIC/everest/blob/master/MARC.Everest.Test/Instances/CustomNamespaceTest.cs

MikeCheel commented 6 years ago

Adding that attribute makes this exception go away but I get new exception "System.InvalidOperationException : Token StartAttribute in state Start would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment."

Any idea where ConformanceLevel is set? I'm just trying inherit from ClinicalDocument and add some additional properties / methods.

MikeCheel commented 6 years ago

I figured it out!

    private const string CustomNamespace = "urn:custom";

    [Structure(Name = "ClinicalDocument", NamespaceUri = CustomNamespace, IsEntryPoint = true)]
    class TestDocument : ClinicalDocument
    {            
    }