Actually, the best solution would be the reverse - storing the value in a Uri property and accessing it with a string.
For example:
public class Server
{
public string Name { get; set; }
[XmlIgnore]
public Uri Uri { get; set; }
[XmlElement("URI")]
public string UriString {
get { return Uri.ToString(); }
set { Uri = new Uri(value); }
}
}
This way, it's impossible to set it to an invalid Uri (which would in your code throw an exception in the property getter). Also, to follow standard .Net casing guidelines, the property should be called Uri
, not URI
.