title: XML array with custom attribute and element names
body: <p>I want to serialize/deserialize xml document in C# like:</p>
  
  <pre><code>&lt;library&gt;
      &lt;my.books genre =""classic""&gt;
           &lt;book title = ""1984"" author=""George Orwell"" /&gt;
           &lt;book title = ""Robinson Crusoe"" author=""Daniel Defoe"" /&gt;
           &lt;book title = ""Frankenstein"" author=""Mary Shelly"" /&gt;
      &lt;/my.books&gt;
  &lt;/library&gt;";
  </code></pre>
  
  <p>There are 2 important things:</p>
  
  <ul>
  <li><p>Element "my.books" must have custom name (not a property name)</p></li>
  <li><p>my.books element must have an attribute ("genre").</p></li>
  </ul>
  
  <p>Here is my code (sample is on <a href="https://dotnetfiddle.net/bH5WVX" rel="nofollow noreferrer">https://dotnetfiddle.net/bH5WVX</a>) :</p>
  
  <pre><code>   using System;
     using System.Xml;
     using System.Xml.Linq;
     using System.Collections.Generic;
     using System.ComponentModel;
     using System.Xml.Serialization;
     using System.IO;
  
     public class Program
     {
      public static void Main()
      {
  
          Library lib = new Library(myBooks: new MyBooks(
              genre: "classic",
              booklist: new List&lt;Book&gt;{
                  new Book("1984", "George Orwell"),
                  new Book("Robinson Crusoe", "Daniel Defoe"),
                  new Book("Oliver Twist", "Mary Shelly"),
  
              }));
  
  
            XmlSerializer formatter = new XmlSerializer(typeof(Library));
  
              using (StringWriter sw = new StringWriter())
              {
                  formatter.Serialize(sw, lib);
                  Console.Write(sw.ToString());
              }
  
          string desiredOutput =
                  @"&lt;library&gt;
  
                      &lt;my.books genre =""classic""&gt;
                          &lt;book title = ""1984"" author=""George Orwell"" /&gt;
                          &lt;book title = ""Robinson Crusoe"" author=""Daniel Defoe"" /&gt;
                          &lt;book title = ""Frankenstein"" author=""Mary Shelly"" /&gt;
                      &lt;/my.books&gt;
  
                  &lt;/library&gt;";            
      }
  
  
  [XmlRoot("library")]    
  public class Library
          {
          public MyBooks MyBooks { get; set; }
  
          [XmlElement("my.books")] 
          public List&lt;Book&gt; Books { get; set; }
  
          public Library()
      {
  
      }
  
      public Library(MyBooks myBooks = null)
      {
          MyBooks = myBooks;
      }
  }
  
  
  [XmlType("my.books")]
  public class MyBooks
  {
       [XmlAttribute("genre")]
          public string Genre { get; set; }
  
       [XmlElement("book")]
          public List&lt;Book&gt; Booklist { get; set; }
  
        public MyBooks(string genre, List&lt;Book&gt; booklist = null)
          {
              Genre = genre;
              Booklist = booklist;
          }
  
            public MyBooks()
          {
  
          }   
  }   
  
  public class Book
      {
          [XmlAttribute("title")]
          public string Title { get; set; }
          [XmlAttribute("author")]
          public string Author { get; set; }
  
          public Book() { }
  
          public Book(string title, string author)
          {
          Title = title;
          Author = author;    
          }
      }
  }
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>&lt;library&gt;
    &lt;MyBooks genre="classic"&gt;
      &lt;book title="1984" author="George Orwell" /&gt;
      &lt;book title="Robinson Crusoe" author="Daniel Defoe" /&gt;
      &lt;book title="Oliver Twist" author="Mary Shelly" /&gt;
    &lt;/MyBooks&gt;
  &lt;/library&gt;
  </code></pre>
  
  <p>The only problem is that I can't force element "MyBooks" to use name "my.books"</p>
  
  <p>I found only one related article on this topic - <a href="http://www.codemeit.com/xml/c-xmlserializer-add-an-attribute-to-an-array-element.html" rel="nofollow noreferrer">http://www.codemeit.com/xml/c-xmlserializer-add-an-attribute-to-an-array-element.html</a>, it suggests to use "XmlType" attribute on class, but it doesn't work here.</p>
  
  <p>Is there any way to apply custom name attribute on this element? </p>