title: Deserialize an XML document with XmlArrayAttribute rows into object correctly using C#
body: <p>I have an XML that looks like this:</p>
  
  <pre><code>&lt;document&gt;
      (...)
      &lt;flex&gt;
          &lt;attrGroupMany name="foodAndBev"&gt;
              &lt;row&gt;
                  &lt;attr name="agency"&gt;ASA EACH CASE36&lt;/attr&gt;
                  &lt;attrQualMany name="ingredientsList"&gt;
                    &lt;value qual="en"&gt;IL EACH CASE3688s&lt;/value&gt;
                    &lt;value qual="en"&gt;IL TEST&lt;/value&gt;
                    &lt;value qual="en"&gt;IL TEST2&lt;/value&gt;
                  &lt;/attrQualMany&gt;
              &lt;/row&gt;
              &lt;row&gt;
                  &lt;attr name="agency"&gt;ASA EACH CASE36&lt;/attr&gt;
                  &lt;attrQualMany name="ingredientsList"&gt;
                    &lt;value qual="en"&gt;IL EACH CASE3688s&lt;/value&gt;
                    &lt;value qual="en"&gt;IL TEST&lt;/value&gt;
                    &lt;value qual="en"&gt;IL TEST2&lt;/value&gt;
                  &lt;/attrQualMany&gt;
              &lt;/row&gt;
          &lt;/attrGroupMany&gt;
          &lt;attr name="FlexType"&gt;food&lt;/attr&gt;
      &lt;/flex&gt;
  &lt;/document&gt;
  </code></pre>
  
  <p>To deserialize this XML into a class, I created the following structures:</p>
  
  <p>The <code>Document</code> class looks like this:</p>
  
  <pre><code>public class document
  {
          (...)
          [System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 158)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attr", typeof(AttrRddType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrMany", typeof(AttrRddManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrQual", typeof(AttrQualRddType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrQualMany", typeof(AttrQualRddManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrQualOpt", typeof(AttrQualOptRddType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrQualOptMany", typeof(AttrQualOptRddManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrGroup", typeof(AttrGroupType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          [System.Xml.Serialization.XmlArrayItemAttribute("attrGroupMany", typeof(AttrGroupManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
          public List&lt;object&gt; flex
          {
              get
              {
                  return this._flex;
              }
              set
              {
                  this._flex = value;
              }
          }
  }
  </code></pre>
  
  <p><code>AttrGroupManyType</code> class looks like:</p>
  
  <pre><code>public class AttrGroupManyType
  {
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string name { get; set; }
  
      [System.Xml.Serialization.XmlArrayAttribute("row", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attr", typeof(AttrRddType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrMany", typeof(AttrRddManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrQual", typeof(AttrQualRddType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrQualMany", typeof(AttrQualRddManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrQualOpt", typeof(AttrQualOptRddType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrQualOptMany", typeof(AttrQualOptRddManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrGroup", typeof(AttrGroupType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      [System.Xml.Serialization.XmlArrayItemAttribute("attrGroupMany", typeof(AttrGroupManyType), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
      public List&lt;object&gt; row { get; set; }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>When defining <strong>AttrGroupManyType</strong> this way, I'm able to deserialize / serialize between an XML string and an object of <code>Document</code> type, but I cannot get more than one <strong>row</strong> per <code>attrGroupMany</code>.</p>
  
  <p>All attributes get merged into <strong>one row</strong>.</p>
  
  <p>For example: foodAndBev, that originally had the following structure:</p>
  
  <p><code>[row0] =&gt; { agency, ingredientsList }</code></p>
  
  <p><code>[row1] =&gt; { agency, ingredientsList }</code></p>
  
  <p>Gets serialized as:</p>
  
  <p><code>[row0] =&gt; { agency, ingredientsList, agency, ingredientsList }</code></p>
  
  <p>Also, if I change the dataType of the <code>row</code> property from <code>List&lt;object&gt;</code> to <code>object[]</code>
  I get only the data in the first row, duplicated attributes don't make it into the output xml.</p>
  
  <p>For the example above, I only get:</p>
  
  <p><code>[row0] =&gt; { agency, ingredientsList }</code></p>
  
  <p><strong>My question is:</strong></p>
  
  <p>How can I change the class implementations to be able to deserialize / serialize mantaining the original data inside <strong>attrGroupMany</strong> nodes, even when multiple <code>row</code>s are present?</p>