title: Removing element from list property in class
body: <p>There is a XML data serialized in C# System.Xml.Serialization.</p>
  
  <p>There is a root class describing root element . It contains fileds of List type described in descendant classes (like Movie).</p>
  
  <p>I can deserialize my XML document into a class, and add a new element in list simply using List.Add or List.Insert:</p>
  
  <pre><code>cfg.Movies.Insert(0, new Movie("Newly added: Pulp Fiction", "1994"));
  </code></pre>
  
  <p>But reverse operation doesn't work:</p>
  
  <pre><code>cfg.Movies.Remove(new Movie("To be removed: The Godfather", "1974"));
  </code></pre>
  
  <p>So the question is how can I remove an element from xml class list?</p>
  
  <p>The sample is here: <a href="https://dotnetfiddle.net/WqCNoD" rel="nofollow noreferrer">https://dotnetfiddle.net/WqCNoD</a></p>
  
  <pre><code>using System;
  using System.IO;
  using System.Xml;
  using System.Xml.Linq;
  using System.Collections.Generic;
  using System.Xml.Serialization;
  
  public class Program
  {
  public static void Main()
  {
  
      string xmlString =
          @"&lt;favorites&gt;
                  &lt;movies&gt;
                      &lt;movie title=""To be removed: The Godfather"" year=""1974"" /&gt;
                      &lt;movie title=""The Terminator"" year=""1984"" /&gt;
                      &lt;movie title=""Dark Knight"" year=""2008"" /&gt;
                  &lt;/movies&gt;
  
                  &lt;books&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;/books&gt;
  
                  &lt;music&gt;
                      &lt;artist title = ""Beatles"" genre=""rock"" /&gt;
                      &lt;artist title = ""Queen"" genre=""rock"" /&gt;
                      &lt;artist title = ""Metallica"" rock=""heavy metal"" /&gt;
                  &lt;/music&gt;
              &lt;/favorites&gt;";
  
      XDocument xdoc = XDocument.Parse(xmlString);
  
      var xdocData = xdoc.ToString();
  
      //DeSerialization
      XmlSerializer serializer = new XmlSerializer(typeof(Favorites));
      using (StringReader reader = new StringReader(xdoc.ToString()))
      {
          Favorites cfg = (Favorites) serializer.Deserialize(reader);
  
  
          // Add &amp; remove element
          cfg.Movies.Insert(0, new Movie("Newly added: Pulp Fiction", "1994"));
          cfg.Movies.Remove(new Movie("To be removed: The Godfather", "1974"));
  
  
          //Serialization
          XmlSerializer serializer2 = new XmlSerializer(cfg.GetType());
  
          using (StringWriter writer = new StringWriter())
          {
              serializer2.Serialize(writer, cfg);
              Console.WriteLine(writer.ToString());
  
          }
  
      }
  }
  
  
  
  [XmlRoot(ElementName = "favorites", DataType = "string", IsNullable = true)]
  public class Favorites
  {
      //public string Name { get; set; }
  
      [XmlArray("movies")]
      [XmlArrayItem("movie")]
      public List&lt;Movie&gt; Movies { get; set; }
  
      public Favorites()
      {
          Movies = new List&lt;Movie&gt;();
      }
  }
  
  
  public class Movie
  {
      [XmlAttribute("title")]
      public string Title { get; set; }
      [XmlAttribute("year")]
      public string Year { get; set; }
  
      public Movie() { }
  
      public Movie(string title, string year)
      {
          Title = title;
          Year = year;
      }
    }
  }
  </code></pre>