Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To solve this problem I had to modify the generated classes:</p> <ol> <li>Move the <code>XmlTextAttribute</code> from the <code>Text</code> property to the <code>Items</code> property and add the parameter <code>Type = typeof(string)</code></li> <li>Remove the <code>Text</code> property</li> <li>Remove the <code>textField</code> field</li> </ol> <p>As a result the <strong>generated code (modified)</strong> 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 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... [System.Xml.Serialization.XmlTextAttribute(typeof(string))] public object[] Items { get { return this.itemsField; } set { this.itemsField = 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>Now 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 array is read like this:</p> <pre><code>itemsField = new object[] { "first line", new StrucDocBr(), new StrucDocBr(), "third line", }; </code></pre> <p>This is <strong>exactly what I need</strong>, the order of the items and their content is <strong>correct</strong>.<br> And if I <strong>serialize</strong> this again, the result is again correct:</p> <pre><code>&lt;paragraph&gt;first line&lt;br /&gt;&lt;br /&gt;third line&lt;/paragraph&gt; </code></pre> <p>What pointed me in the right direction was the answer by Guillaume, I also thought that it must be possible like this. And then there was this in the <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltextattribute(VS.85).aspx" rel="noreferrer">MSDN documentation to <code>XmlTextAttribute</code></a>: </p> <blockquote> <p>You can apply the <strong>XmlTextAttribute</strong> to a field or property that returns an array of strings. <em>You can also apply the attribute to an array of type Object but you must set the Type property to string. In that case, any strings inserted into the array are serialized as XML text.</em></p> </blockquote> <p>So the serialization and deserialization work correct now, but I don't know if there are any other side effects. Maybe it's not possible to generate a schema from these classes with xsd.exe anymore, but I don't need that anyway.</p>
 

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