/// <summary>
        /// Parse ProfileExtensionDefnComponent
        /// </summary>
        public static Hl7.Fhir.Model.Profile.ProfileExtensionDefnComponent ParseProfileExtensionDefnComponent(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.Profile.ProfileExtensionDefnComponent existingInstance = null)
        {
            Hl7.Fhir.Model.Profile.ProfileExtensionDefnComponent result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.Profile.ProfileExtensionDefnComponent();
            string currentElementName = reader.CurrentElementName;

            reader.EnterElement();

            while (reader.HasMoreElements())
            {
                var atName = reader.CurrentElementName;
                // Parse element extension
                if (atName == "extension")
                {
                    result.Extension = new List <Hl7.Fhir.Model.Extension>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extension"))
                    {
                        result.Extension.Add(ExtensionParser.ParseExtension(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element _id
                else if (atName == "_id")
                {
                    result.LocalIdElement = Id.Parse(reader.ReadPrimitiveContents(typeof(Id)));
                }

                // Parse element code
                else if (atName == "code")
                {
                    result.CodeElement = CodeParser.ParseCode(reader, errors);
                }

                // Parse element contextType
                else if (atName == "contextType")
                {
                    result.ContextTypeElement = CodeParser.ParseCode <Hl7.Fhir.Model.Profile.ExtensionContext>(reader, errors);
                }

                // Parse element context
                else if (atName == "context")
                {
                    result.ContextElement = new List <Hl7.Fhir.Model.FhirString>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "context"))
                    {
                        result.ContextElement.Add(FhirStringParser.ParseFhirString(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element definition
                else if (atName == "definition")
                {
                    result.Definition = ProfileParser.ParseElementDefinitionComponent(reader, errors);
                }

                else
                {
                    errors.Add(String.Format("Encountered unknown element {0} while parsing {1}", reader.CurrentElementName, currentElementName), reader);
                    reader.SkipSubElementsFor(currentElementName);
                    result = null;
                }
            }

            reader.LeaveElement();
            return(result);
        }
        /// <summary>
        /// Parse ElementDefinitionComponent
        /// </summary>
        public static Hl7.Fhir.Model.Profile.ElementDefinitionComponent ParseElementDefinitionComponent(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.Profile.ElementDefinitionComponent existingInstance = null)
        {
            Hl7.Fhir.Model.Profile.ElementDefinitionComponent result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.Profile.ElementDefinitionComponent();
            string currentElementName = reader.CurrentElementName;

            reader.EnterElement();

            while (reader.HasMoreElements())
            {
                var atName = reader.CurrentElementName;
                // Parse element extension
                if (atName == "extension")
                {
                    result.Extension = new List <Hl7.Fhir.Model.Extension>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extension"))
                    {
                        result.Extension.Add(ExtensionParser.ParseExtension(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element _id
                else if (atName == "_id")
                {
                    result.LocalIdElement = Id.Parse(reader.ReadPrimitiveContents(typeof(Id)));
                }

                // Parse element short
                else if (atName == "short")
                {
                    result.ShortElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element formal
                else if (atName == "formal")
                {
                    result.FormalElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element comments
                else if (atName == "comments")
                {
                    result.CommentsElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element requirements
                else if (atName == "requirements")
                {
                    result.RequirementsElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element synonym
                else if (atName == "synonym")
                {
                    result.SynonymElement = new List <Hl7.Fhir.Model.FhirString>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "synonym"))
                    {
                        result.SynonymElement.Add(FhirStringParser.ParseFhirString(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element min
                else if (atName == "min")
                {
                    result.MinElement = IntegerParser.ParseInteger(reader, errors);
                }

                // Parse element max
                else if (atName == "max")
                {
                    result.MaxElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element type
                else if (atName == "type")
                {
                    result.Type = new List <Hl7.Fhir.Model.Profile.TypeRefComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "type"))
                    {
                        result.Type.Add(ProfileParser.ParseTypeRefComponent(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element nameReference
                else if (atName == "nameReference")
                {
                    result.NameReferenceElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element value
                else if (atName.StartsWith("value"))
                {
                    result.Value = FhirParser.ParseElement(reader, errors);
                }

                // Parse element example
                else if (atName.StartsWith("example"))
                {
                    result.Example = FhirParser.ParseElement(reader, errors);
                }

                // Parse element maxLength
                else if (atName == "maxLength")
                {
                    result.MaxLengthElement = IntegerParser.ParseInteger(reader, errors);
                }

                // Parse element condition
                else if (atName == "condition")
                {
                    result.ConditionElement = new List <Hl7.Fhir.Model.Id>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "condition"))
                    {
                        result.ConditionElement.Add(IdParser.ParseId(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element constraint
                else if (atName == "constraint")
                {
                    result.Constraint = new List <Hl7.Fhir.Model.Profile.ElementDefinitionConstraintComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "constraint"))
                    {
                        result.Constraint.Add(ProfileParser.ParseElementDefinitionConstraintComponent(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element mustSupport
                else if (atName == "mustSupport")
                {
                    result.MustSupportElement = FhirBooleanParser.ParseFhirBoolean(reader, errors);
                }

                // Parse element isModifier
                else if (atName == "isModifier")
                {
                    result.IsModifierElement = FhirBooleanParser.ParseFhirBoolean(reader, errors);
                }

                // Parse element binding
                else if (atName == "binding")
                {
                    result.BindingElement = FhirUriParser.ParseFhirUri(reader, errors);
                }

                // Parse element mapping
                else if (atName == "mapping")
                {
                    result.Mapping = new List <Hl7.Fhir.Model.Profile.ElementDefinitionMappingComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "mapping"))
                    {
                        result.Mapping.Add(ProfileParser.ParseElementDefinitionMappingComponent(reader, errors));
                    }

                    reader.LeaveArray();
                }

                else
                {
                    errors.Add(String.Format("Encountered unknown element {0} while parsing {1}", reader.CurrentElementName, currentElementName), reader);
                    reader.SkipSubElementsFor(currentElementName);
                    result = null;
                }
            }

            reader.LeaveElement();
            return(result);
        }
        /// <summary>
        /// Parse Profile
        /// </summary>
        public static Hl7.Fhir.Model.Profile ParseProfile(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.Profile existingInstance = null)
        {
            Hl7.Fhir.Model.Profile result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.Profile();
            string currentElementName     = reader.CurrentElementName;

            reader.EnterElement();

            while (reader.HasMoreElements())
            {
                var atName = reader.CurrentElementName;
                // Parse element extension
                if (atName == "extension")
                {
                    result.Extension = new List <Hl7.Fhir.Model.Extension>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extension"))
                    {
                        result.Extension.Add(ExtensionParser.ParseExtension(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element language
                else if (atName == "language")
                {
                    result.LanguageElement = CodeParser.ParseCode(reader, errors);
                }

                // Parse element text
                else if (atName == "text")
                {
                    result.Text = NarrativeParser.ParseNarrative(reader, errors);
                }

                // Parse element contained
                else if (atName == "contained")
                {
                    result.Contained = new List <Hl7.Fhir.Model.Resource>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "contained"))
                    {
                        result.Contained.Add(ParserUtils.ParseContainedResource(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element _id
                else if (atName == "_id")
                {
                    result.LocalIdElement = Id.Parse(reader.ReadPrimitiveContents(typeof(Id)));
                }

                // Parse element identifier
                else if (atName == "identifier")
                {
                    result.IdentifierElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element version
                else if (atName == "version")
                {
                    result.VersionElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element name
                else if (atName == "name")
                {
                    result.NameElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element publisher
                else if (atName == "publisher")
                {
                    result.PublisherElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element telecom
                else if (atName == "telecom")
                {
                    result.Telecom = new List <Hl7.Fhir.Model.Contact>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "telecom"))
                    {
                        result.Telecom.Add(ContactParser.ParseContact(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element description
                else if (atName == "description")
                {
                    result.DescriptionElement = FhirStringParser.ParseFhirString(reader, errors);
                }

                // Parse element code
                else if (atName == "code")
                {
                    result.Code = new List <Hl7.Fhir.Model.Coding>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "code"))
                    {
                        result.Code.Add(CodingParser.ParseCoding(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element status
                else if (atName == "status")
                {
                    result.StatusElement = CodeParser.ParseCode <Hl7.Fhir.Model.Profile.ResourceProfileStatus>(reader, errors);
                }

                // Parse element experimental
                else if (atName == "experimental")
                {
                    result.ExperimentalElement = FhirBooleanParser.ParseFhirBoolean(reader, errors);
                }

                // Parse element date
                else if (atName == "date")
                {
                    result.DateElement = FhirDateTimeParser.ParseFhirDateTime(reader, errors);
                }

                // Parse element fhirVersion
                else if (atName == "fhirVersion")
                {
                    result.FhirVersionElement = IdParser.ParseId(reader, errors);
                }

                // Parse element structure
                else if (atName == "structure")
                {
                    result.Structure = new List <Hl7.Fhir.Model.Profile.ProfileStructureComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "structure"))
                    {
                        result.Structure.Add(ProfileParser.ParseProfileStructureComponent(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element extensionDefn
                else if (atName == "extensionDefn")
                {
                    result.ExtensionDefn = new List <Hl7.Fhir.Model.Profile.ProfileExtensionDefnComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "extensionDefn"))
                    {
                        result.ExtensionDefn.Add(ProfileParser.ParseProfileExtensionDefnComponent(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element binding
                else if (atName == "binding")
                {
                    result.Binding = new List <Hl7.Fhir.Model.Profile.ProfileBindingComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "binding"))
                    {
                        result.Binding.Add(ProfileParser.ParseProfileBindingComponent(reader, errors));
                    }

                    reader.LeaveArray();
                }

                else
                {
                    errors.Add(String.Format("Encountered unknown element {0} while parsing {1}", reader.CurrentElementName, currentElementName), reader);
                    reader.SkipSubElementsFor(currentElementName);
                    result = null;
                }
            }

            reader.LeaveElement();
            return(result);
        }
        internal static Resource ParseResource(IFhirReader reader, ErrorList errors)
        {
            try
            {
                reader.MoveToContent();

                if (ParserUtils.IsAtFhirElement(reader, "AdverseReaction"))
                {
                    return(AdverseReactionParser.ParseAdverseReaction(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Alert"))
                {
                    return(AlertParser.ParseAlert(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "AllergyIntolerance"))
                {
                    return(AllergyIntoleranceParser.ParseAllergyIntolerance(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "CarePlan"))
                {
                    return(CarePlanParser.ParseCarePlan(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Condition"))
                {
                    return(ConditionParser.ParseCondition(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Conformance"))
                {
                    return(ConformanceParser.ParseConformance(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Coverage"))
                {
                    return(CoverageParser.ParseCoverage(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Device"))
                {
                    return(DeviceParser.ParseDevice(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "DeviceCapabilities"))
                {
                    return(DeviceCapabilitiesParser.ParseDeviceCapabilities(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "DeviceLog"))
                {
                    return(DeviceLogParser.ParseDeviceLog(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "DeviceObservation"))
                {
                    return(DeviceObservationParser.ParseDeviceObservation(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "DiagnosticOrder"))
                {
                    return(DiagnosticOrderParser.ParseDiagnosticOrder(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "DiagnosticReport"))
                {
                    return(DiagnosticReportParser.ParseDiagnosticReport(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Document"))
                {
                    return(DocumentParser.ParseDocument(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "DocumentReference"))
                {
                    return(DocumentReferenceParser.ParseDocumentReference(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Encounter"))
                {
                    return(EncounterParser.ParseEncounter(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "FamilyHistory"))
                {
                    return(FamilyHistoryParser.ParseFamilyHistory(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Group"))
                {
                    return(GroupParser.ParseGroup(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "ImagingStudy"))
                {
                    return(ImagingStudyParser.ParseImagingStudy(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Immunization"))
                {
                    return(ImmunizationParser.ParseImmunization(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "ImmunizationProfile"))
                {
                    return(ImmunizationProfileParser.ParseImmunizationProfile(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "List"))
                {
                    return(ListParser.ParseList(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Location"))
                {
                    return(LocationParser.ParseLocation(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Media"))
                {
                    return(MediaParser.ParseMedia(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Medication"))
                {
                    return(MedicationParser.ParseMedication(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "MedicationAdministration"))
                {
                    return(MedicationAdministrationParser.ParseMedicationAdministration(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "MedicationDispense"))
                {
                    return(MedicationDispenseParser.ParseMedicationDispense(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "MedicationPrescription"))
                {
                    return(MedicationPrescriptionParser.ParseMedicationPrescription(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "MedicationStatement"))
                {
                    return(MedicationStatementParser.ParseMedicationStatement(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Message"))
                {
                    return(MessageParser.ParseMessage(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Observation"))
                {
                    return(ObservationParser.ParseObservation(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "OperationOutcome"))
                {
                    return(OperationOutcomeParser.ParseOperationOutcome(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Order"))
                {
                    return(OrderParser.ParseOrder(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "OrderResponse"))
                {
                    return(OrderResponseParser.ParseOrderResponse(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Organization"))
                {
                    return(OrganizationParser.ParseOrganization(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Other"))
                {
                    return(OtherParser.ParseOther(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Patient"))
                {
                    return(PatientParser.ParsePatient(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Practitioner"))
                {
                    return(PractitionerParser.ParsePractitioner(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Procedure"))
                {
                    return(ProcedureParser.ParseProcedure(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Profile"))
                {
                    return(ProfileParser.ParseProfile(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Provenance"))
                {
                    return(ProvenanceParser.ParseProvenance(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Query"))
                {
                    return(QueryParser.ParseQuery(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Questionnaire"))
                {
                    return(QuestionnaireParser.ParseQuestionnaire(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "RelatedPerson"))
                {
                    return(RelatedPersonParser.ParseRelatedPerson(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "SecurityEvent"))
                {
                    return(SecurityEventParser.ParseSecurityEvent(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Specimen"))
                {
                    return(SpecimenParser.ParseSpecimen(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Substance"))
                {
                    return(SubstanceParser.ParseSubstance(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Supply"))
                {
                    return(SupplyParser.ParseSupply(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "ValueSet"))
                {
                    return(ValueSetParser.ParseValueSet(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Binary"))
                {
                    return(BinaryParser.ParseBinary(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "Appointment"))
                {
                    return(AppointmentParser.ParseAppointment(reader, errors));
                }
                else if (ParserUtils.IsAtFhirElement(reader, "InterestOfCare"))
                {
                    return(InterestOfCareParser.ParseInterestOfCare(reader, errors));
                }
                else
                {
                    errors.Add(String.Format("Encountered unrecognized resource '{0}'", reader.CurrentElementName), reader);
                    reader.SkipSubElementsFor(reader.CurrentElementName);
                    return(null);
                }
            }
            catch (Exception xe)
            {
                errors.Add(xe.Message, reader);
                return(null);
            }
        }