简体   繁体   English

具有默认值的XmlSerializer属性转换器

[英]XmlSerializer property converter with a default value

I was using the sample code specified in the link below to convert the xml value which is a string "Y" into a boolean value for the property it is mapped against. 我正在使用下面的链接中指定的示例代码将作为字符串“ Y”的xml值转换为其映射所针对的属性的布尔值。 However in my xml that is being serialized the xml element may not exist but I still want to map it to a property in my object. 但是,在我要序列化的xml中,xml元素可能不存在,但是我仍然想将其映射到对象中的属性。 Curently after serialization this property has a null value but I want to set it to false. 序列化后,当前该属性具有空值,但我想将其设置为false。 How can I do this using the example in the link below. 如何使用下面链接中的示例执行此操作。

I did try this by setting it in the constructor for the property but then I get an error. 我确实通过在属性的构造函数中对其进行设置来进行尝试,但是随后出现错误。 Cannot implicitly convert type 'bool' to 'BoolYN' 无法将类型'bool'隐式转换为'BoolYN'

XmlSerializer property converter XmlSerializer属性转换器

If I understand you, you should implement IXmlSerializable 如果我了解您,则应该实现IXmlSerializable

http://msdn.microsoft.com/pl-pl/library/system.xml.serialization.ixmlserializable%28v=vs.110%29.aspx http://msdn.microsoft.com/pl-pl/library/system.xml.serialization.ixmlserializable%28v=vs.110%29.aspx

And write your own interpretation of "Y" to bool and use [XmlIgnore] for "unused" property. 然后将自己对“ Y”的解释写给bool,并为未使用的属性使用[XmlIgnore]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM