Search code examples
c#xmlbiztalkhl7-fhirdstu2-fhir

Change default namespace when Serializing to XML fhir-net-api


I'm using the the fhir-net-api library found here to transform FHIR messages from JSON to XML. The resulting XML has a namespace of http://hl7.org/fhir/ which seems to be the default used by the library. Is there a way to control the namespace when serializing to XML using this library. I know I can change the XML namespace separately but it would be nice to do in one step.

var b = new
          Hl7.Fhir.Serialization.ResourceReader(
            FhirJsonParser.CreateFhirReader(json),
                       ParserSettings.Default).Deserialize();

       return Hl7.Fhir.Serialization.FhirSerializer.SerializeResourceToXml(b).ToString();

Solution

  • According to the FHIR specification, one of the rules for the XML representation of FHIR resources is:

    FHIR elements are always in the namespace http://hl7.org/fhir . This is usually specified as the default namespace on the root element.

    That is why the FhirSerializer includes this namespace when serializing to XML. You cannot change that behaviour, so if you really need to (question: why?), you'll need to change it after serialization.