title: Implementing Custom XML Serialization/Deserialization of compound data type?
body: <p>In our application, we have a Money type, which contains an amount (decimal) and a currency code (string).  In simple form, it looks like this:</p>
  
  <pre><code>public class Money
  {
    public decimal Amount{get;set;}
    public string CurrencyCode{get;set;}
  }
  </code></pre>
  
  <p>As you might imagine, this gets used in many places in the application, and is frequently serialized/deserialized as it is sent to/from the client.  The Money amount is typically serialized as a compound value, e.g. "1.23USD" to represent $1.23.  The old client (HTML/JS) would parse that value into its component parts back into the Money type.  Money values are sent as element values, and as attribute values, depending on where they are in the app, e.g:</p>
  
  <pre><code>&lt;SomeClass moneyValue="1.23USD" ... /&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre><code> &lt;SomeClass&gt;
  &lt;MoneyValue&gt;1.23USD&lt;/MoneyValue&gt;
  ...
  &lt;/SomeClass&gt;
  </code></pre>
  
  <p>I am trying to figure out a way that I can use the built-in C#/.NET Xml Serialization tools to have this same sort of behavior.  I looked at implementing ISerializable, but haven't been able to figure out quite the correct way to do so.</p>
  
  <p>Essentially, I want to be able to have the Money amount deserialized by custom logic of my own (that knows how to parse "1.23USD" into a Money amount), and serialize it to the simple string, e.g. "1.23USD"</p>
  
  <p>The end goal would be to able to have the Money amount in a class as either:</p>
  
  <pre><code>[XmlAttribute]
  public Money SomeField // SomeField='123.USD
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>[XmlElement]
  public Money SomeOtherField //&lt;SomeOtherField&gt;1.23USD&lt;/SomeOtherField&gt;
  </code></pre>
  
  <p>just the same way you can do that with simple types like int, string, double, etc.</p>
  
  <p>Is this possible?</p>