/// <summary>
        /// Parse Message
        /// </summary>
        public static Hl7.Fhir.Model.Message ParseMessage(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.Message existingInstance = null)
        {
            Hl7.Fhir.Model.Message result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.Message();
            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 = IdParser.ParseId(reader, errors);
                }

                // Parse element timestamp
                else if (atName == "timestamp")
                {
                    result.TimestampElement = InstantParser.ParseInstant(reader, errors);
                }

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

                // Parse element response
                else if (atName == "response")
                {
                    result.Response = MessageParser.ParseMessageResponseComponent(reader, errors);
                }

                // Parse element source
                else if (atName == "source")
                {
                    result.Source = MessageParser.ParseMessageSourceComponent(reader, errors);
                }

                // Parse element destination
                else if (atName == "destination")
                {
                    result.Destination = MessageParser.ParseMessageDestinationComponent(reader, errors);
                }

                // Parse element enterer
                else if (atName == "enterer")
                {
                    result.Enterer = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element author
                else if (atName == "author")
                {
                    result.Author = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element receiver
                else if (atName == "receiver")
                {
                    result.Receiver = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element responsible
                else if (atName == "responsible")
                {
                    result.Responsible = ResourceReferenceParser.ParseResourceReference(reader, errors);
                }

                // Parse element effective
                else if (atName == "effective")
                {
                    result.Effective = PeriodParser.ParsePeriod(reader, errors);
                }

                // Parse element reason
                else if (atName == "reason")
                {
                    result.Reason = CodeableConceptParser.ParseCodeableConcept(reader, errors);
                }

                // Parse element data
                else if (atName == "data")
                {
                    result.Data = new List <Hl7.Fhir.Model.ResourceReference>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "data"))
                    {
                        result.Data.Add(ResourceReferenceParser.ParseResourceReference(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);
        }
        public static void SerializeMessage(Hl7.Fhir.Model.Message value, IFhirWriter writer, bool summary)
        {
            writer.WriteStartRootObject("Message");
            writer.WriteStartComplexContent();

            // Serialize element _id
            if (value.LocalIdElement != null)
            {
                writer.WritePrimitiveContents("_id", value.LocalIdElement, XmlSerializationHint.Attribute);
            }

            // Serialize element extension
            if (value.Extension != null && !summary && value.Extension.Count > 0)
            {
                writer.WriteStartArrayElement("extension");
                foreach (var item in value.Extension)
                {
                    writer.WriteStartArrayMember("extension");
                    ExtensionSerializer.SerializeExtension(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element language
            if (value.LanguageElement != null && !summary)
            {
                writer.WriteStartElement("language");
                CodeSerializer.SerializeCode(value.LanguageElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element text
            if (value.Text != null && !summary)
            {
                writer.WriteStartElement("text");
                NarrativeSerializer.SerializeNarrative(value.Text, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element contained
            if (value.Contained != null && !summary && value.Contained.Count > 0)
            {
                writer.WriteStartArrayElement("contained");
                foreach (var item in value.Contained)
                {
                    writer.WriteStartArrayMember("contained");
                    FhirSerializer.SerializeResource(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element identifier
            if (value.IdentifierElement != null && !summary)
            {
                writer.WriteStartElement("identifier");
                IdSerializer.SerializeId(value.IdentifierElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element timestamp
            if (value.TimestampElement != null && !summary)
            {
                writer.WriteStartElement("timestamp");
                InstantSerializer.SerializeInstant(value.TimestampElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element event
            if (value.EventElement != null && !summary)
            {
                writer.WriteStartElement("event");
                CodeSerializer.SerializeCode(value.EventElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element response
            if (value.Response != null && !summary)
            {
                writer.WriteStartElement("response");
                MessageSerializer.SerializeMessageResponseComponent(value.Response, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element source
            if (value.Source != null && !summary)
            {
                writer.WriteStartElement("source");
                MessageSerializer.SerializeMessageSourceComponent(value.Source, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element destination
            if (value.Destination != null && !summary)
            {
                writer.WriteStartElement("destination");
                MessageSerializer.SerializeMessageDestinationComponent(value.Destination, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element enterer
            if (value.Enterer != null && !summary)
            {
                writer.WriteStartElement("enterer");
                ResourceReferenceSerializer.SerializeResourceReference(value.Enterer, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element author
            if (value.Author != null && !summary)
            {
                writer.WriteStartElement("author");
                ResourceReferenceSerializer.SerializeResourceReference(value.Author, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element receiver
            if (value.Receiver != null && !summary)
            {
                writer.WriteStartElement("receiver");
                ResourceReferenceSerializer.SerializeResourceReference(value.Receiver, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element responsible
            if (value.Responsible != null && !summary)
            {
                writer.WriteStartElement("responsible");
                ResourceReferenceSerializer.SerializeResourceReference(value.Responsible, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element effective
            if (value.Effective != null && !summary)
            {
                writer.WriteStartElement("effective");
                PeriodSerializer.SerializePeriod(value.Effective, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element reason
            if (value.Reason != null && !summary)
            {
                writer.WriteStartElement("reason");
                CodeableConceptSerializer.SerializeCodeableConcept(value.Reason, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element data
            if (value.Data != null && !summary && value.Data.Count > 0)
            {
                writer.WriteStartArrayElement("data");
                foreach (var item in value.Data)
                {
                    writer.WriteStartArrayMember("data");
                    ResourceReferenceSerializer.SerializeResourceReference(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }


            writer.WriteEndComplexContent();
            writer.WriteEndRootObject();
        }