title: System.Uri implements ISerializable, but gives error?
body: <blockquote>
  <p><strong>Possible Duplicate:</strong><br />
  <a href="https://stackoverflow.com/questions/1036492/how-to-xml-serialize-a-uri">How to (xml) serialize a uri</a></p>
  </blockquote>
  
  <p>To my knowledge <code>Uri</code> implements ISerializable, but throws error when used like this:</p>
  <pre><code>XmlSerializer xs = new XmlSerializer(typeof(Server));
  xs.Serialize(Console.Out, new Server { Name = &quot;test&quot;, URI = new Uri(&quot;http://localhost/&quot;) });
  
  public class Server
  {
      public string Name { get; set; }
      public Uri URI { get; set; }
  }
  </code></pre>
  <p>Works just fine if <code>Uri</code> type is changed to <code>string</code>.</p>
  <p>Anyone knows what is the culprit?</p>
  <hr />
  <h2>Solution proposed by <em>Anton Gogolev</em>:</h2>
  <pre><code>public class Server
  {
      public string Name { get; set; }
  
      [XmlIgnore()]
      public Uri Uri; 
      
      [XmlElement(&quot;URI&quot;)]
      public string _URI // Unfortunately this has to be public to be xml serialized.
      { 
          get { return Uri.ToString(); }
          set { Uri = new Uri(value); }
      }
  }
  </code></pre>
  <p>(Thanks for <em>SLaks</em> also pointing out the backwardness of my method...)</p>
  <p>This produces XML output:</p>
  <pre><code>&lt;Server&gt;
      &lt;URI&gt;http://localhost/&lt;/URI&gt;
      &lt;Name&gt;test&lt;/Name&gt;
  &lt;/Server&gt;
  </code></pre>
  <p>I rewrote it here so the code is visible.</p>