XML序列化布尔值介于0和1 [英] XML Serialize boolean as 0 and 1
问题描述
XML架构第2部分规定,被定义为布尔数据类型的实例可以有以下法律文字{真,假,1,0}。
下面的XML,例如,反序列化时,布尔财产效仿设置为真正
。
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>
然而,当我序列化对象返回到XML,我得到真
而不是数值。我的问题是,有没有我可以控制XML布尔表示一种方式?
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?
推荐答案
您可以实施的IXmlSerializable这将让你无论你想改变你的类的序列化输出。这将需要创建3种方法的getSchema()的ReadXml(XmlReader中R)和中WriteXML(XmlWriter的R)。当您实现接口,这些方法调用,而不是.NET试图序列化对象本身。
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:
例子可以发现,在.UK /显示/ 4639 /相对=nofollow> http://www.developerfusion.co.uk/show/4639/ 以及
http://www.developerfusion.co.uk/show/4639/ and
http://msdn.microsoft.com/en-us /library/system.xml.serialization.ixmlserializable.aspx
这篇关于XML序列化布尔值介于0和1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!