Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, here is my final solution (hope it helps someone), that can serialize a plain array, List&lt;>, HashSet&lt;>, ...</p> <p>To achieve this, we'll need to tell the serializer what root node to use, and it's kind of tricky...</p> <p>1) Use 'XmlType' on the serializable object</p> <pre><code>[XmlType("link")] public class LinkFinalVersion { [XmlAttribute("href")] public string Url { get; set; } [XmlAttribute("rel")] public string Relationship { get; set; } } </code></pre> <p>2) Code a 'smart-root-detector-for-collection' method, that will return a XmlRootAttribute</p> <pre><code>private XmlRootAttribute XmlRootForCollection(Type type) { XmlRootAttribute result = null; Type typeInner = null; if(type.IsGenericType) { var typeGeneric = type.GetGenericArguments()[0]; var typeCollection = typeof (ICollection&lt;&gt;).MakeGenericType(typeGeneric); if(typeCollection.IsAssignableFrom(type)) typeInner = typeGeneric; } else if(typeof (ICollection).IsAssignableFrom(type) &amp;&amp; type.HasElementType) { typeInner = type.GetElementType(); } // yeepeeh ! if we are working with a collection if(typeInner != null) { var attributes = typeInner.GetCustomAttributes(typeof (XmlTypeAttribute), true); if((attributes != null) &amp;&amp; (attributes.Length &gt; 0)) { var typeName = (attributes[0] as XmlTypeAttribute).TypeName + 's'; result = new XmlRootAttribute(typeName); } } return result; } </code></pre> <p>3) Push that XmlRootAttribute into the serializer</p> <pre><code>// hack : get the XmlRootAttribute if the item is a collection var root = XmlRootForCollection(type); // create the serializer var serializer = new XmlSerializer(type, root); </code></pre> <p>I told you it was tricky ;)</p> <hr> <p>To improve this, you can :</p> <p>A) Create a XmlTypeInCollectionAttribute to specify a custom root name (If the basic pluralization does not fit your need)</p> <pre><code>[XmlType("link")] [XmlTypeInCollection("links")] public class LinkFinalVersion { } </code></pre> <p>B) If possible, cache your XmlSerializer (in a static Dictionary for example).</p> <p>In my testing, instanciating a XmlSerializer without the XmlRootAttributes takes 3ms. If you specify an XmlRootAttribute, it takes around 80ms (Just to have a custom root node name !)</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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