XML将布尔值序列化为0和1 [英] XML Serialize boolean as 0 and 1
问题描述
XML模式第2部分指定定义为boolean的数据类型的实例可以具有以下法定文字{true,false,1,0}。
例如,当反序列化时,以下XML将布尔属性Emulate设置为 true
。
< root>
< emulate> 1< / emulate>
< / root>但是,当我将对象序列化回XML时,我会得到 true(
)
而不是数值。我的问题是,是否有一种方法可以控制XML中的布尔表示?解决方案您可以实现IXmlSerializable允许你改变你想要的类的序列化输出。这将需要创建3个方法GetSchema(),ReadXml(XmlReader r)和WriteXml(XmlWriter r)。当实现接口时,调用这些方法而不是.NET尝试序列化对象本身。
例如:
http://www.developerfusion.co.uk/show/4639/ 和
http://msdn.microsoft.com/en -us / library / system.xml.serialization.ixmlserializable.aspx
The XML Schema Part 2 specifies that an instance of a datatype that is defined as boolean can have the following legal literals {true, false, 1, 0}.
The following XML, for example, when deserialized, sets the boolean property "Emulate" to true
.
<root>
<emulate>1</emulate>
</root>
However, when I serialize the object back to the XML, I get true
instead of the numerical value. My question is, is there a way that I can control the boolean representation in the XML?
解决方案 You can implement IXmlSerializable which will allow you to alter the serialized output of your class however you want. This will entail creating the 3 methods GetSchema(), ReadXml(XmlReader r) and WriteXml(XmlWriter r). When you implement the interface, these methods are called instead of .NET trying to serialize the object itself.
Examples can be found at:
http://www.developerfusion.co.uk/show/4639/ and
http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx
这篇关于XML将布尔值序列化为0和1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!