首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >.NET中“混合”类型的正确XML序列化和反序列化

.NET中“混合”类型的正确XML序列化和反序列化
EN

Stack Overflow用户
提问于 2010-04-02 23:16:32
回答 3查看 24.1K关注 0票数 18

我当前的任务是编写一个用于处理HL7 CDA文件的类库。

这些XML文件是具有定义的HL7模式的.NET文件,因此我使用xsd.exe生成用于XML序列化和反序列化的.NET类。

XML Schema包含各种类型,这些类型包含mixed="true“属性,指定此类型的XML节点可以包含与其他XML节点混合的普通文本。

其中一种类型的XML schema的相关部分如下所示:

代码语言:javascript
复制
<xs:complexType name="StrucDoc.Paragraph" mixed="true">
    <xs:sequence>
        <xs:element name="caption" type="StrucDoc.Caption" minOccurs="0"/>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="br" type="StrucDoc.Br"/>
            <xs:element name="sub" type="StrucDoc.Sub"/>
            <xs:element name="sup" type="StrucDoc.Sup"/>
            <!-- ...other possible nodes... -->
        </xs:choice>
    </xs:sequence>
    <xs:attribute name="ID" type="xs:ID"/>
    <!-- ...other attributes... -->
</xs:complexType>

此类型的生成的代码如下所示:

代码语言:javascript
复制
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName="StrucDoc.Paragraph", Namespace="urn:hl7-org:v3")]
public partial class StrucDocParagraph {

    private StrucDocCaption captionField;

    private object[] itemsField;

    private string[] textField;

    private string idField;

    // ...fields for other attributes...

    /// <remarks/>
    public StrucDocCaption caption {
        get {
            return this.captionField;
        }
        set {
            this.captionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("br", typeof(StrucDocBr))]
    [System.Xml.Serialization.XmlElementAttribute("sub", typeof(StrucDocSub))]
    [System.Xml.Serialization.XmlElementAttribute("sup", typeof(StrucDocSup))]
    // ...other possible nodes...
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string[] Text {
        get {
            return this.textField;
        }
        set {
            this.textField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="ID")]
    public string ID {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    // ...properties for other attributes...
}

如果我对元素进行反序列化,其中的段落节点如下所示:

代码语言:javascript
复制
<paragraph>first line<br /><br />third line</paragraph>

结果是这样读取项目和文本数组的:

代码语言:javascript
复制
itemsField = new object[]
{
    new StrucDocBr(),
    new StrucDocBr(),
};
textField = new string[]
{
    "first line",
    "third line",
};

因此,无法确定文本和其他节点的确切顺序。

如果我再次对此进行序列化,结果将如下所示:

代码语言:javascript
复制
<paragraph>
    <br />
    <br />first linethird line
</paragraph>

默认的序列化程序只是先序列化项目,然后序列化文本。

我尝试在StrucDocParagraph类上实现IXmlSerializable,以便可以控制内容的反序列化和序列化,但这相当复杂,因为涉及的类太多了,而且我还没有找到解决方案,因为我不知道付出的努力是否有回报。

对于这个问题,有没有某种简单的解决方案,或者甚至可以通过IXmlSerializable进行自定义序列化?或者我应该只使用XmlDocumentXmlReader/XmlWriter来处理这些文档?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2567414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档