public static void SerializeMedicationAdministration(Hl7.Fhir.Model.MedicationAdministration value, IFhirWriter writer, bool summary)
        {
            writer.WriteStartRootObject("MedicationAdministration");
            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.Identifier != null && !summary && value.Identifier.Count > 0)
            {
                writer.WriteStartArrayElement("identifier");
                foreach (var item in value.Identifier)
                {
                    writer.WriteStartArrayMember("identifier");
                    IdentifierSerializer.SerializeIdentifier(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

            // Serialize element status
            if (value.StatusElement != null && !summary)
            {
                writer.WriteStartElement("status");
                CodeSerializer.SerializeCode <Hl7.Fhir.Model.MedicationAdministration.MedicationAdministrationStatus>(value.StatusElement, writer, summary);
                writer.WriteEndElement();
            }

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

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

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

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

            // Serialize element wasNotGiven
            if (value.WasNotGivenElement != null && !summary)
            {
                writer.WriteStartElement("wasNotGiven");
                FhirBooleanSerializer.SerializeFhirBoolean(value.WasNotGivenElement, writer, summary);
                writer.WriteEndElement();
            }

            // Serialize element reasonNotGiven
            if (value.ReasonNotGiven != null && !summary && value.ReasonNotGiven.Count > 0)
            {
                writer.WriteStartArrayElement("reasonNotGiven");
                foreach (var item in value.ReasonNotGiven)
                {
                    writer.WriteStartArrayMember("reasonNotGiven");
                    CodeableConceptSerializer.SerializeCodeableConcept(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }

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

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

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

            // Serialize element dosage
            if (value.Dosage != null && !summary && value.Dosage.Count > 0)
            {
                writer.WriteStartArrayElement("dosage");
                foreach (var item in value.Dosage)
                {
                    writer.WriteStartArrayMember("dosage");
                    MedicationAdministrationSerializer.SerializeMedicationAdministrationDosageComponent(item, writer, summary);
                    writer.WriteEndArrayMember();
                }
                writer.WriteEndArrayElement();
            }


            writer.WriteEndComplexContent();
            writer.WriteEndRootObject();
        }
        /// <summary>
        /// Parse MedicationAdministration
        /// </summary>
        public static Hl7.Fhir.Model.MedicationAdministration ParseMedicationAdministration(IFhirReader reader, ErrorList errors, Hl7.Fhir.Model.MedicationAdministration existingInstance = null)
        {
            Hl7.Fhir.Model.MedicationAdministration result = existingInstance != null ? existingInstance : new Hl7.Fhir.Model.MedicationAdministration();
            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.Identifier = new List <Hl7.Fhir.Model.Identifier>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "identifier"))
                    {
                        result.Identifier.Add(IdentifierParser.ParseIdentifier(reader, errors));
                    }

                    reader.LeaveArray();
                }

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

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

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

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

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

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

                // Parse element reasonNotGiven
                else if (atName == "reasonNotGiven")
                {
                    result.ReasonNotGiven = new List <Hl7.Fhir.Model.CodeableConcept>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "reasonNotGiven"))
                    {
                        result.ReasonNotGiven.Add(CodeableConceptParser.ParseCodeableConcept(reader, errors));
                    }

                    reader.LeaveArray();
                }

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

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

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

                    while (ParserUtils.IsAtArrayElement(reader, "administrationDevice"))
                    {
                        result.AdministrationDevice.Add(ResourceReferenceParser.ParseResourceReference(reader, errors));
                    }

                    reader.LeaveArray();
                }

                // Parse element dosage
                else if (atName == "dosage")
                {
                    result.Dosage = new List <Hl7.Fhir.Model.MedicationAdministration.MedicationAdministrationDosageComponent>();
                    reader.EnterArray();

                    while (ParserUtils.IsAtArrayElement(reader, "dosage"))
                    {
                        result.Dosage.Add(MedicationAdministrationParser.ParseMedicationAdministrationDosageComponent(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);
        }