Here is a code sample that creates the Ensemble representation of an HL7 message from a string and sets it's metadata according to the information found in the MSH segment (using HL7 version 2.5 schema):

/// Create EnsLib.HL7.Message object from HL7 string
Method CreateHL7Message(pHL7String as %String, Output pHL7 As EnsLib.HL7.Message) As %Status
    #Dim sc as %Status
    #Dim ex as %Exception.AbstractException
    #Dim tMSH as EnsLib.HL7.Segment    
    Try    {
        // Create HL7 message object
        set pHL7=##class(EnsLib.HL7.Message).ImportFromString(pHL7String,.sc) $$$ThrowOnError(sc)
        // Determine DocType from MSH
        set tMSH=pHL7.getSegmentByIndex(1)
        set tName=tMSH.GetValueAt(9,":_~\&")
        set tDocType=##class(EnsLib.HL7.Schema).ResolveSchemaTypeToDocType("2.5",tName, sc) $$$ThrowOnError(sc)
        // Set DocType and build map
        set pHL7.DocType=tDocType
        set sc=pHL7.BuildMap()    $$$ThrowOnError(sc)
    Catch ex
        // don't return HL7 object in case of an error
        kill pHL7
        set sc=ex.AsStatus()
    Quit sc