Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect XML serialization and deserialization of "mixed" types in .NET
    primarykey
    data
    text
    <p>My current task involves writing a class library for processing HL7 CDA files.<br> These HL7 CDA files are XML files with a defined XML schema, so I used xsd.exe to generate .NET classes for XML serialization and deserialization.</p> <p>The XML Schema contains various types which contain the <strong>mixed="true" attribute</strong>, specifying that an XML node of this type may contain normal text mixed with other XML nodes.<br> The relevant part of the <strong>XML schema</strong> for one of these types looks like this:</p> <pre><code>&lt;xs:complexType name="StrucDoc.Paragraph" mixed="true"&gt; &lt;xs:sequence&gt; &lt;xs:element name="caption" type="StrucDoc.Caption" minOccurs="0"/&gt; &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt; &lt;xs:element name="br" type="StrucDoc.Br"/&gt; &lt;xs:element name="sub" type="StrucDoc.Sub"/&gt; &lt;xs:element name="sup" type="StrucDoc.Sup"/&gt; &lt;!-- ...other possible nodes... --&gt; &lt;/xs:choice&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="ID" type="xs:ID"/&gt; &lt;!-- ...other attributes... --&gt; &lt;/xs:complexType&gt; </code></pre> <p>The <strong>generated code</strong> for this type looks like this:</p> <pre><code>/// &lt;remarks/&gt; [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... /// &lt;remarks/&gt; public StrucDocCaption caption { get { return this.captionField; } set { this.captionField = value; } } /// &lt;remarks/&gt; [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; } } /// &lt;remarks/&gt; [System.Xml.Serialization.XmlTextAttribute()] public string[] Text { get { return this.textField; } set { this.textField = value; } } /// &lt;remarks/&gt; [System.Xml.Serialization.XmlAttributeAttribute(DataType="ID")] public string ID { get { return this.idField; } set { this.idField = value; } } // ...properties for other attributes... } </code></pre> <p>If I <strong>deserialize</strong> an XML element where the paragraph node looks like this:</p> <pre><code>&lt;paragraph&gt;first line&lt;br /&gt;&lt;br /&gt;third line&lt;/paragraph&gt; </code></pre> <p>The <strong>result</strong> is that the item and text arrays are read like this:</p> <pre><code>itemsField = new object[] { new StrucDocBr(), new StrucDocBr(), }; textField = new string[] { "first line", "third line", }; </code></pre> <p>From this there is no possible way to determine the exact order of the text and the other nodes.<br> If I <strong>serialize</strong> this again, the result looks exactly like this:</p> <pre><code>&lt;paragraph&gt; &lt;br /&gt; &lt;br /&gt;first linethird line &lt;/paragraph&gt; </code></pre> <p>The default serializer just serializes the items first and then the text.</p> <p>I tried implementing <code>IXmlSerializable</code> on the StrucDocParagraph class so that I could control the deserialization and serialization of the content, but it's rather complex since there are so many classes involved and I didn't come to a solution yet because I don't know if the effort pays off.</p> <p>Is there some kind of <strong>easy workaround</strong> to this problem, or is it even possible by doing custom serialization via <code>IXmlSerializable</code>? Or should I just use <code>XmlDocument</code> or <code>XmlReader</code>/<code>XmlWriter</code> to process these documents?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload