tags:

views:

109

answers:

1

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"&gt;
    <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"&gt;
    <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?

+1  A: 

A simple way is to run the XSD.EXE program against the schema. This will produce a set of classes. The XML Serializer can be used to load data from your XML file into class instances, and to serialize them back out to XML once you've changed them.


Looks like it claims to produce C++ code.

/language:
    The language to use for the generated code. Choose from 'CS', 'VB', 'JS',
    'VJS', 'CPP' or provide a fully-qualified name for a class implementing
    System.CodeDom.Compiler.CodeDomProvider. The default language
    is 'CS' (CSharp). Short form is '/l:'.
John Saunders
XSD.exe won't produce C++ classes, so I don't think this will work.
Velociraptors
Check my edit: /l:CPP
John Saunders
Interesting. The information from /? does include CPP as an option, but the MSDN documentation (http://msdn.microsoft.com/en-us/library/x6c1kb0s%28VS.80%29.aspx) does not:"Choose from CS (C#, which is the default), VB (Visual Basic), JS (JScript), or VJS (Visual J#)."
Velociraptors
Did you realize that the (VS80) means you're looking at old information? On the right-hand side of that article near the top, there's a panel to get other versions. Take a look and see if the 3.5 version fixes the documentation.
John Saunders
I'm using VS 2005/.NET 2.0, so the old information is what I need. The 3.5 documentation says the same thing.
Velociraptors
I recommend you use the later, as it's more likely to have been updated. It's not likely to be specific to 3.5 without saying so. Since the documentation is wrong, you should use the Feedback link to say so.
John Saunders