title: How to serialize a sequence in c# (xs sequence with maxOccurs different that 0)
body: <p>I'm having an issue to serialize an array in C# to be compliant with a XSD.</p>
  
  <p>I need to serialize an array, where each property of each child would be listed into one single list, to be compliant with a XSD which define something like this:</p>
  
  <pre><code>&lt;xs:sequence minOccurs="0" maxOccurs="unbounded"&gt;
      &lt;xs:element name="Id" type="xs:string"/&gt;
      &lt;xs:element name="Value" type="xs:string"/&gt;
  &lt;/xs:sequence&gt;
  </code></pre>
  
  <p>Exemple, I've got this:</p>
  
  <pre><code>&lt;ServerRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;Time&gt;11-08-15 08:27:31&lt;/Time&gt;
    &lt;RequestContent&gt;
      &lt;Id&gt;myId1&lt;/Id&gt;
      &lt;Value&gt;myValue1&lt;/Value&gt;
    &lt;/RequestContent&gt;
    &lt;RequestContent&gt;
      &lt;Id&gt;myId2&lt;/Id&gt;
      &lt;Value&gt;myValue2&lt;/Value&gt;
    &lt;/RequestContent&gt;
  &lt;/ServerRequest&gt;
  </code></pre>
  
  <p>And what I need is this:</p>
  
  <pre><code>&lt;ServerRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;Time&gt;11-08-15 08:27:31&lt;/Time&gt;
    &lt;Id&gt;myId1&lt;/Id&gt;
    &lt;Value&gt;myValue1&lt;/Value&gt;
    &lt;Id&gt;myId2&lt;/Id&gt;
    &lt;Value&gt;myValue2&lt;/Value&gt;
  &lt;/ServerRequest&gt;
  </code></pre>
  
  <p>Here is my code sample:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          ServerRequest test = new ServerRequest();
          test.Time = DateTime.UtcNow.ToString();
          test.RequestContent = new List&lt;ServerRequestContent&gt;()
          {
              new ServerRequestContent() { Id = "myId1", Value = "myValue1"},
              new ServerRequestContent() { Id = "myId2", Value = "myValue2"}
          };
  
          using (StringWriter sw = new StringWriter())
          {
              XmlSerializer xmlSerializer = new XmlSerializer(typeof(ServerRequest));
              xmlSerializer.Serialize(sw, test);
  
              Console.WriteLine(sw.ToString());
          }
      }
  }
  
  
  [XmlRoot("ServerRequest")]
  public class ServerRequest
  {
      [XmlElement()]
      public string Time { get; set; }
  
      [XmlElement]
      public List&lt;ServerRequestContent&gt; RequestContent { get; set; }
  }
  
  public class ServerRequestContent
  {
      [XmlElement()]
      public string Id { get; set; }
  
      [XmlElement()]
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>I've been trying for several hours, but still can't find a solution. Best thing I found so far is this: <a href="https://stackoverflow.com/questions/4774465/serialize-array-without-root-element">Serialize Array without root element</a>, but I would have to change a lot of thing in the C# classes generated from the XSD, which I don't really want to.</p>
  
  <p>Thanks for any help</p>
  
  <p><strong><em>Solution:</em></strong></p>
  
  <p>Implementing IXmlSerializable was probably the easiest way to deal with this. ServerRequest class now looks like this:</p>
  
  <pre><code>[XmlRoot("ServerRequest")]
  public class ServerRequest : IXmlSerializable
  {
      [XmlElement()]
      public string Time { get; set; }
  
      [XmlElement]
      public List&lt;ServerRequestContent&gt; RequestContent { get; set; }
  
      #region IXmlSerializable
  
      public XmlSchema GetSchema()
      {
          return null;
      }
  
      public void ReadXml(XmlReader reader)
      {
          //...
      }
  
      public void WriteXml(XmlWriter writer)
      {
          writer.WriteElementString("Time", Time);
  
          foreach (ServerRequestContent content in RequestContent)
          {
              writer.WriteElementString("Id", content.Id);
              writer.WriteElementString("Value", content.Value);
          }
      }
  
      #endregion
  }
  </code></pre>