通过.NET读取/写入XML来处理数据

时间:2009-07-16 18:34:15

标签: .net c++ xml

我正在研究一个使用数学模型进行生理模拟的项目。我们目前使用JS MML中定义的模型,JSim的数学建模语言,因为JSim被研究人员广泛使用。基本上,模型包含变量(具有公式,初始值和可选单位)和常量参数(具有值和可选单位)。变量和参数在内部由相同的数据类型表示,因为两者之间的唯一区别在于它是否具有公式。

我们还允许变量具有MML不支持的额外关联数据(解剖信息)。用户可以加载标准MML模型并根据需要进行编辑。保存这些编辑后,如果使用MML语法保存模型,则会丢失额外数据。

我目前的解决方案是将模型保存为XML而不是MML文本文件:

<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>

这是我正在使用的架构:

<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>

有更好的方法吗?我没有太多生成XML的经验,我从来没有写过架构,所以我不知道是否有任何严重的问题。

以编程方式生成XML的最佳方法是什么?现在我正在使用XmlDocument。如何在文档中设置架构位置? XSD文件将与应用程序捆绑在一起,而不是托管在服务器上。

一旦我生成了XML,我该如何阅读/加载它?我见过的大部分信息都建议LINQ,但项目使用的是.NET 2.0和C ++。 XmlReader是最好的选择吗?或者我应该使用System.Xml.Serialization进行读写?我错过了什么重要的事吗?

1 个答案:

答案 0 :(得分:1)

一种简单的方法是针对模式运行XSD.EXE程序。这将产生一组类。 XML Serializer可用于将XML文件中的数据加载到类实例中,并在更改后将它们序列化为XML。


看起来它声称生成C ++代码。

/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:'.
相关问题