Generating C# classes from FHIR schemas

c#, hl7-fhir, xsd

Solution

So far I have been able to generate classes and de-serialize many of the patient*.xml samples provided to both raw classes generated as described, and classes generated from the raw classes by a SOAP service.

Editing xhtml1-strict.xsd to fix this issue is not that simple. I used xsd.exe to attempt to create classes from the file, then used the error messages as starting points. After some experimentation, I cam up with this file. It addresses the problem with the div element, as long the HTML contained is kept simple. I am sharing the difference report for others to make use of. The numbers represent line-number. (I am just sharing the changes because of size limitations, I tried to share the whole file).

XSD\xhtml1-strict.xsd(413):      <!--<xs:group ref="inline"/>-->
XSD\xhtml1-strict.xsd(441):      <!--<xs:element ref="pre"/>-->
XSD\xhtml1-strict.xsd(443):      <!--<xs:element ref="blockquote"/>-->
XSD\xhtml1-strict.xsd(462):      <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(519):      <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(520):      <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(539):      <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(1349):        <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(1351):        <!--<xs:group ref="inline"/>-->
XSD\xhtml1-strict.xsd(1352):        <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(1450):          <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(1452):          <!--<xs:group ref="misc"/>-->
XSD\xhtml1-strict.xsd(1718):          <!--<xs:group ref="block"/>-->
XSD\xhtml1-strict.xsd(1720):          <!--<xs:group ref="inline"/>-->
XSD\xhtml1-strict.xsd(1721):          <!--<xs:group ref="misc"/>-->

I am also sharing my notes so far regarding manual edits required, to fix issues in the classes generated.

Generate entities with Xsd2Code add-in from www.codeplex.com\Xsd2Code

Use fhir-atom-single.xsd as the source XSD
Use Parms:
    Serilization.GenerateXMLAttributes = true
    Code.Namespace = Hl7.Fhir.Validation.SchematronOutput
    Collection.CollectionObjectType=Array

!!! Do not open Schema in Designer, or classes will change.

Manual updates:

    public partial class boolean : Element
    ...
        [System.Xml.Serialization.XmlAttributeAttribute("value")]
        public bool Value
        {


    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = false, Namespace = "http://www.w3.org/1999/xhtml")]
    public partial class div : Flow


    Refactor:
    public partial class FeedType
    to
    public partial class feed

Problem

I am attempting to generate c# classes from schemas provided by the FHIR project: http://hl7.org/implement/standards/fhir/ I have downloaded the schemas: http://hl7.org/documentcenter/public/standards/FHIR/fhir-all-xsd.zip I have "Unblocked" the zip file and unzipped the xsd files into a folder. While attempting to use xsd.exe to create c# classes, I keep getting errors that indicate an issue with the schemas. Consistently getting xhtml:div element is not declared in addition to others. The file fhir-all.xsd seems to list the top level objects. I was able to get the simple schema tombstone.xsd to work with xsd.exe, but a more complex item like valueset.xsd or alert.xsd fails miserably. I can't see what is wrong with these files. Any help on how to fix these schemas will be appreciated.

Original source

Related problems