Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Xml serialization, collection and root element
    text
    copied!<p>My app serializes objects in streams. Here is a sample of what I need :</p> <pre><code>&lt;links&gt; &lt;link href="/users" rel="users" /&gt; &lt;link href="/features" rel="features" /&gt; &lt;/links&gt; </code></pre> <p>In this case, the object is a collection of 'links' object.</p> <p>-----------First version</p> <p>At first I used the <strong>DataContractSerializer</strong>, however you cannot serialize members as attributes (<a href="https://stackoverflow.com/questions/591907/how-can-you-control-wcf-serialization-so-it-uses-attributes-instead-of-elements">source</a>)</p> <p>Here is the object :</p> <pre><code>[DataContract(Name="link")] public class LinkV1 { [DataMember(Name="href")] public string Url { get; set; } [DataMember(Name="rel")] public string Relationship { get; set; } } </code></pre> <p>And here is the result :</p> <pre><code>&lt;ArrayOflink xmlns:i="...." xmlns="..."&gt; &lt;link&gt; &lt;href&gt;/users&lt;/href&gt; &lt;rel&gt;users&lt;/rel&gt; &lt;/link&gt; &lt;link&gt; &lt;href&gt;/features&lt;/href&gt; &lt;rel&gt;features&lt;/rel&gt; &lt;/link&gt; &lt;/ArrayOflink&gt; </code></pre> <p>----------- Second version</p> <p>Ok, not quiet what I want, so I tried the classic <strong>XmlSerializer</strong>, but... oh nooo, you cannot specify the name of the root element &amp; of the collection's elements if the root element is a collection...</p> <p>Here is the code :</p> <pre><code>[XmlRoot("link")] public class LinkV2 { [XmlAttribute("href")] public string Url { get; set; } [XmlAttribute("rel")] public string Relationship { get; set; } } </code></pre> <p>Here is the result :</p> <pre><code>&lt;ArrayOfLinkV2&gt; &lt;LinkV2 href="/users" rel="users" /&gt; &lt;LinkV2 href="/features" rel="features" /&gt; &lt;LinkV2 href="/features/user/{keyUser}" rel="featuresByUser" /&gt; &lt;/ArrayOfLinkV2&gt; </code></pre> <p>----------- Third version</p> <p>using <strong>XmlSerializer</strong> + a root element :</p> <pre><code>[XmlRoot("trick")] public class TotallyUselessClass { [XmlArray("links"), XmlArrayItem("link")] public List&lt;LinkV2&gt; Links { get; set; } } </code></pre> <p>And its result :</p> <pre><code> &lt;trick&gt; &lt;links&gt; &lt;link href="/users" rel="users" /&gt; &lt;link href="/features" rel="features" /&gt; &lt;link href="/features/user/{keyUser}" rel="featuresByUser" /&gt; &lt;/links&gt; &lt;/trick&gt; </code></pre> <p>Nice, but I don't want that root node !! I want my collection to be the root node.</p> <p>Here are the contraints :</p> <ul> <li>the serialization code is generic, it works with anything serializable</li> <li>the inverse operation (deserialization) have to work too</li> <li>I don't want to regex the result (I serialize directly in an output stream)</li> </ul> <p>What are my solutions now :</p> <ol> <li>Coding my own XmlSerializer</li> <li>Trick XmlSerializer when it works with a collection (I tried, having it find a XmlRootElement and plurialize it to generate its own XmlRootAttribute, but that causes problem when deserializing + the items name still keeps the class name)</li> </ol> <p>Any idea ?</p> <p>What really bother me in that issue, is that what I want seems to be really really really simple...</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