Disable an empty XML element as Guid.Empty

I have problems with deserialization.

<Order> ... <CardNumber /> ... </Order> 

If i use

 <CardNumber>00000000-0000-0000-0000-000000000000</CardNumber> 

It works fine, but in the case when I use only <CardNumber /> , the object does not deserialize (

Is there a way to deserialize an empty element as Guid.Empty?

The property that should be displayed with the value of this element:

 [XmlElement(ElementName = "CardNumber")] [JsonProperty("CardNumber")] public Guid? CardNumber { get; set; } 

The same situation in JSON works fine and uses the value of Guid.Empty instead of the value of an empty element

 { "CardNumber": "" } 
+5
source share
2 answers

The exception you see clearly explains the problem:

 System.InvalidOperationException occurred Message="There is an error in XML document (3, 3)." InnerException: System.FormatException Message="Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)." 

As stated, XmlSerializer does not support deserializing an empty string in Guid . Thus, you will need to do the conversion manually using the surrogate property:

 [XmlRoot("Order")] public class Order { [XmlIgnore] [JsonProperty("CardNumber")] public Guid? CardNumber { get; set; } [XmlElement(ElementName = "CardNumber", IsNullable = true)] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)] [JsonIgnore] public string XmlCardNumber { get { if (CardNumber == null) return null; else if (CardNumber.Value == Guid.Empty) return ""; return XmlConvert.ToString(CardNumber.Value); } set { if (value == null) CardNumber = null; else if (string.IsNullOrEmpty(value)) CardNumber = Guid.Empty; else CardNumber = XmlConvert.ToGuid(value); } } } 

If this is what you need to do in many different types having Guid? properties Guid? , you can extract the surrogate type as follows:

 [XmlType(AnonymousType = true, IncludeInSchema = false)] public class XmlGuid { [XmlIgnore] public Guid Guid { get; set; } [XmlText] public string XmlCardNumber { get { if (Guid == Guid.Empty) return null; return XmlConvert.ToString(Guid); } set { if (string.IsNullOrEmpty(value)) Guid = Guid.Empty; else Guid = XmlConvert.ToGuid(value); } } public static implicit operator Guid?(XmlGuid x) { if (x == null) return null; return x.Guid; } public static implicit operator XmlGuid(Guid? g) { if (g == null) return null; return new XmlGuid { Guid = g.Value }; } public static implicit operator Guid(XmlGuid x) { if (x == null) return Guid.Empty; return x.Guid; } public static implicit operator XmlGuid(Guid g) { return new XmlGuid { Guid = g }; } } 

And use it like:

 [XmlRoot("Order")] public class Order { [XmlElement(Type = typeof(XmlGuid), ElementName = "CardNumber", IsNullable = true)] [JsonProperty("CardNumber")] public Guid? CardNumber { get; set; } } 

Here I use the fact that the XmlElementAttribute.Type property automatically captures the implicit conversion I defined for Guid? from and to XmlGuid .

+1
source

Null is not the same as Guid.Empty . In a JSON serializer, you denote null using an empty string.

If you serialize your class using XmlSerializer , you will see that it uses xsi:nil="true" to indicate a null value.

For instance:

 <Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <CardNumber xsi:nil="true" /> </Order> 
+3
source

All Articles