I'm working on a project that performs physiological simulations using mathematical models. We currently use models defined in MML, JSim's Mathematical Modeling Language, since JSim widely used by researchers. Basically, a model contains variables (with a formula, initial value, and optional units) and constant parameters (with a value and optional units). Variables and parameters are internally represented by the same data type, since the only difference between the two is whether or not it has a formula.
We also allow variables to have extra associated data (anatomical information) that isn't supported by MML. Users can load a standard MML model and edit it as needed. When those edits are saved, the extra data is lost if the model is saved using MML syntax.
My current solution is to save the model as XML rather than an MML text file:
<model xmlns="http://tempuri.org/model_schema.xsd">
<name>sample_model</name>
<description>String</description>
<variable type="realDomain" constant="false">
<name>ID_1</name>
<formula>G_p/VG</formula>
<value>0.75</value>
<units>s^-1</units>
<description>String</description>
<anatomical_structure FMAID="62970">
<name>Kidney</name>
</anatomical_structure>
</variable>
<variable type="real" constant="true">
<name>ID_2</name>
<formula />
<value>1000</value>
<units>mg</units>
</variable>
</model>
This is the schema I'm using:
<xs:schema id="model_schema" targetNamespace="http://tempuri.org/model_schema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/model_schema.xsd" xmlns:mstns="http://tempuri.org/model_schema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="model">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="description" type="xs:string" minOccurs="0" />
<xs:element name="variable" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:ID" />
<xs:element name="formula" type="xs:string" />
<xs:element name="value" type="xs:float" />
<xs:element name="units" type="xs:string" minOccurs="0" />
<xs:element name="description" type="xs:string" minOccurs="0" />
<xs:element name="anatomical_structure" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
</xs:sequence>
<xs:attribute name="FMAID" type="xs:int" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="constant" type="xs:boolean" use="required" />
<xs:attribute name="type" type="var_type" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="var_type">
<xs:restriction base="xs:string">
<xs:enumeration value="realDomain" />
<xs:enumeration value="real" />
<xs:enumeration value="int" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Is there a better way to do this? I don't have much experience generating XML and I've never written a schema, so I don't know whether there are any serious problems.
What's the best way to programmatically generate the XML? Right now I'm using XmlDocument. How do I set the schema location in the document? The XSD file will be bundled with the application rather than hosted on a server.
Once I've generated the XML, how do I read/load it? Most of the information I've seen suggests LINQ, but project uses .NET 2.0 and C++. Is XmlReader the best option? Or should I use System.Xml.Serialization for both reading and writing? Have I missed anything important?