title: How do you find out when you've been loaded via XML Serialization?
body: <p>I'm trying to load a tree of objects via XML serialisation, and at the moment it will load the objects in, and create the tree quite happily. My issue revolves around the fact that these classes support a level of auditing. What I'd like to be able to do is call some method on each object after it has finished being loaded.</p>
  
  <p>For the sake of argument, assume I have a fairly generic object tree with differing classes at different levels, like:</p>
  
  <pre><code> &lt;Customer name="Foo Bar Inc."&gt;
     &lt;Office IsHq="True"&gt;
       &lt;Street&gt;123 Any Street&lt;/Street&gt;
       &lt;Town name="Anytown"&gt;
         &lt;State name="Anystate"&gt;
           &lt;Country name="My Country" /&gt;
         &lt;/State&gt;
       &lt;/Town&gt;
     &lt;/Office&gt;
     &lt;Office IsHq="False"&gt;
       &lt;Street&gt;456 High Street&lt;/Street&gt;
       &lt;Town name="Anycity"&gt;
         &lt;State name="Anystate"&gt;
           &lt;Country name="My Country" /&gt;
         &lt;/State&gt;
       &lt;/Town&gt;
     &lt;/Office&gt;
   &lt;/Customer&gt;
  </code></pre>
  
  <p>Is there any way using the default serialisers (In the similar way that you can create methods like <code>ShouldSerializeFoo</code>) to determine when loading has finished for each object?</p>
  
  <p><b>Edit:</b>
  I should point out that the obvious case of exposing something akin to an <code>OnLoaded()</code> method that I <em>could</em> call after deserialising, strikes me as being a "bad thing to do".</p>
  
  <p><b>Edit2:</b>
  For the sake of discussion this is my current <strike>hack</strike> "approach", which works for the basic level, but the child City node still thinks it needs to be saved with changes (in the real world the object model is a lot more complex, but this will at least compile, without the need for full source)</p>
  
  <pre><code>public class Office
  {
      [XmlAttribute("IsHq")]
      public bool IsHeadquarters { get; set; }
  
      [XmlElement]
      public string Street { get; set; }
  
      [XmlElement]
      public Town Town { get; set; }
  
      protected virtual void OnLoaded() {}
  
      public static OfficeCollection Search()
      {
          OfficeCollection retval = new OfficeCollection();
          string xmlString = @"
                      &lt;Office IsHq='True'&gt;
                          &lt;Street&gt;123 Any Street&lt;/Street&gt;
                          &lt;Town name='Anytown'&gt;
                              &lt;State name='Anystate'&gt;
                                  &lt;Country name='My Country' /&gt;
                              &lt;/State&gt;
                          &lt;/Town&gt;
                      &lt;/Office&gt;";
  
          XmlSerializer xs = new XmlSerializer(retval.GetType());
          XmlReader xr = new XmlTextReader(xmlString);
          retval = (OfficeCollection)xs.Deserialize(xr);
  
          foreach (Office thisOffice in retval)
          {
              thisOffice.OnLoaded();
          }
          return retval;
      }
  }
  </code></pre>