[英]Deserializing a boolean value with nullable attribute?
I am deserializing a string back to an object using C#. 我正在使用C#将字符串反序列化回对象。 The xml string looks like
xml字符串看起来像
"<Authentication xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
<Status>Success</Status>
<Available i:nil=\"true\"/>
</Authentication>"
While I'm managed to handle the Available by making the bool property nullable, I'm just wondering what is the correct way to handle i:nil=\\"true\\"? 通过将bool属性设置为可空值来设法处理Available的同时,我只是想知道处理i:nil = \\“ true \\”的正确方法是什么?
Should I being doing something else other than just making a property Available property nullable? 除了将属性Available属性设置为可空值外,我还应该做其他事情吗?
Note that without making the bool property nullable, it throws me an error ie string "" cannot be converted to bool, so making nullable sorted my problem, but I'm just curious as to whether or not I should be doing more than just making this nullable 请注意,如果不将bool属性设置为可空状态,则会引发错误,即字符串“”无法转换为bool,因此使nullable可以解决我的问题,但我只是想知道是否应该做更多的事情这个可为空
Thanks. 谢谢。
Your property should be nullable. 您的媒体资源应该可以为空。 The XML is explicitly telling you that the property can be either
true
, false
, or null
; XML明确告诉您该属性可以为
true
, false
或null
; the 3 are valid values and your class should support all 3. 3是有效值,您的班级应该支持所有3。
You don't need to do anything other than making your property a bool?
除了使您的资产变得
bool?
之外,您不需要执行其他任何操作bool?
; ; the
XmlSerializer
will properly handle reading/writing the i:nil="true"
when the property is null
. XmlSerializer
将在属性为null
时正确处理i:nil="true"
读写。
Just make sure that your code is designed to account for the null
values where applicable so you don't get exceptions. 只要确保您的代码设计为在适用的情况下考虑
null
值,就不会出现异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.