I'm working with the .NET Serialization support. I need to use the Google Geocoding API to retrieve the results of a geocoding query as XML, and deserialize the XML to a C# class. The problem is, the C# class has a property that matches to an XML element that may or may not be present in the XML stream.
I've looked through the MSDN documentation for XML serialization/deserialization for a way to handle this, but nothing jumps out. Is there a way to specify that an element is optional in the XML stream?
Here is the C# class to contain the deserialized XML:
[XmlRoot]
public class MyGeocodeResponse
{
[XmlElement("status")]
public string Status { get; set; }
[XmlElement("result")]
public Result[] Results { get; set; }
[XmlElement("partial_match")]
public bool PartialMatch { get; set; }
}
The "partial_match" element appears to be optional. When I deserialize some XML that does not have the "partial_match" element, an exception is thrown (InvalidOperationException).
Is there a way to specify that the "partial_match" element may not be present?