0

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?

kmontgom
  • 1,419
  • 13
  • 18
  • May be this one will help you https://stackoverflow.com/questions/25867210/deserialize-xml-with-optional-tags – Code First Oct 11 '18 at 20:22
  • Doesn't appear to work. I tried the IsNullable setting for XmlElement, as well as the DefaultValue attribute, and got the same exception. – kmontgom Oct 11 '18 at 20:35
  • I can't reproduce the crash, see https://dotnetfiddle.net/0LFGOT. If the `` element is missing then `PartialMatch` is left at its default value, which is `false`. Can you share a [mcve] that reproduces the exception, including your code and the XML? If you need to *track* whether `` was present in the XML file, and prevent it from being serialized back if not present, see [ShouldSerialize*() vs *Specified Conditional Serialization Pattern](https://stackoverflow.com/q/37838640/3744182). – dbc Oct 14 '18 at 19:36

2 Answers2

0

Did you try to use DataContract and [DataMember(IsRequired = false) instead?

[DataContract(Namespace ="youNamespace")]
public class MyGeocodeResponse
{
[DataMember(Name="status")]
public string Status { get; set; }

[DataMember(Name="result")]
public Result[] Results { get; set; }

[DataMember(Name="partial_match", IsRequired = false)]
public bool PartialMatch { get; set; }
}
R. Fajkowski
  • 61
  • 1
  • 2
  • 1
    This answer is not XmlSerializer but DataContractSerializer. OP is asking about XmlSerializer. – CodingYoshi Oct 11 '18 at 21:18
  • That's true, my mistake. Do you mind me asking in that case, what is the difference between both of them, since both serializer's can create XML file from instance of the class? Thanks!! – R. Fajkowski Oct 12 '18 at 14:32
  • Please see [this](https://stackoverflow.com/questions/2505778/datacontractserializer-vs-xmlserializer-pros-and-cons-of-each-serializer) to understand the difference. But even if there was no difference, the OP has decided to use XmlSerializer so the answer has to be about that. – CodingYoshi Oct 12 '18 at 16:45
  • Amazing. Thanks for that!! – R. Fajkowski Oct 14 '18 at 09:09
0

If the element may be present but it may have a Null value, then use this:

[XmlElement("partial_match", IsNullable = true)]

If the element may not be present at all, then do this:

private bool? partialMatch;
[XmlElement("partial_match")]
public bool PartialMatch 
{ 
    get { return this.partialMatch; }

    set { this.partialMatch = value; this.PartialMatchSpecified = true; } 
}

[XmlIgnore] 
public bool PartialMatchSpecified { get; set; }
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64