作者:紫色咖啡调 | 来源:互联网 | 2023-05-18 00:40
I have some troubles with deserializing.
我有一些反序列化的麻烦。
...
...
If I use
如果我使用
00000000-0000-0000-0000-000000000000
it's working normally, but in case when I use just
- object is not deserializing (
它正常工作,但是如果我只使用
- 对象没有反序列化(
Is there any way to deserialize empty element as Guid.Empty?
有没有办法将空元素反序列化为Guid.Empty?
Property which should be mapped with value of this element:
应该使用此元素的值映射的属性:
[XmlElement(ElementName = "CardNumber")]
[JsonProperty("CardNumber")]
public Guid? CardNumber { get; set; }
Same situation in JSON works normally and use Guid.Empty instead of empty element value
JSON中的相同情况正常工作并使用Guid.Empty而不是空元素值
{
"CardNumber": ""
}
2 个解决方案
1
The exception you are seeing explains the problem clearly:
您看到的例外情况清楚地解释了问题:
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 to a Guid
. Thus you will need to do the conversion manually using a surrogate property:
如上所述,XmlSerializer不支持将空字符串反序列化为Guid。因此,您需要使用代理属性手动进行转换:
[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 something you need to do in many different types that have Guid?
properties, you can extract a surrogate type like so:
如果您需要在许多具有Guid的不同类型中执行此操作?属性,你可以提取代理类型,如下所示:
[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 am taking advantage of the fact that the XmlElementAttribute.Type
property automatically picks up the implicit conversion I defined for Guid?
from and to XmlGuid
.
在这里,我利用了XmlElementAttribute.Type属性自动获取我为Guid定义的隐式转换这一事实?来自和到XmlGuid。