C# XML 将布尔值序列化为 0 和 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/84449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:24:08  来源:igfitidea点击:

XML Serialize boolean as 0 and 1

提问by Igal Tabachnik

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.

XML 模式第 2 部分指定定义为布尔值的数据类型的实例可以具有以下合法文字 {true, false, 1, 0}。例如,以下 XML 在反序列化时将布尔属性“Emulate”设置为true.

<root>
    <emulate>1</emulate>
</root>

However, when I serialize the object back to the XML, I get trueinstead of the numerical value. My question is, is there a way that I can control the boolean representation in the XML?

但是,当我将对象序列化回 XML 时,我得到的true不是数值。我的问题是,有没有办法控制 XML 中的布尔表示?

采纳答案by Wolfwyrd

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.

您可以实现 IXmlSerializable,这将允许您根据需要更改类的序列化输出。这将需要创建 3 个方法 GetSchema()、ReadXml(XmlReader r) 和 WriteXml(XmlWriter r)。实现接口时,将调用这些方法,而不是 .NET 尝试序列化对象本身。

Examples can be found at:

可以在以下位置找到示例:

http://www.developerfusion.co.uk/show/4639/and

http://www.developerfusion.co.uk/show/4639/

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

回答by mdb

No, not using the default System.Xml.XmlSerializer: you'd need to change the data type to an int to achieve that, or muck around with providing your own serialization code (possible, but not much fun).

不,不使用默认的 System.Xml.XmlSerializer:您需要将数据类型更改为 int 以实现这一点,或者提供您自己的序列化代码(可能,但没有多少乐趣)。

However, you can simply post-process the generated XML instead, of course, either using XSLT, or simply using string substitution. A bit of a hack, but pretty quick, both in development time and run time...

但是,您可以简单地对生成的 XML 进行后处理,当然,可以使用 XSLT,也可以简单地使用字符串替换。有点hack,但在开发时间和运行时间方面都很快......

回答by Simon Steele

You can also do this by using some XmlSerializer attribute black magic:

你也可以通过使用一些 XmlSerializer 属性黑魔法来做到这一点:

[XmlIgnore]
public bool MyValue { get; set; }

/// <summary>Get a value purely for serialization purposes</summary>
[XmlElement("MyValue")]
public string MyValueSerialize
{
    get { return this.MyValue ? "1" : "0"; }
    set { this.MyValue = XmlConvert.ToBoolean(value); }
}

You can also use other attributes to hide this member from intellisense if you're offended by it! It's not a perfect solution, but it can be quicker than implementing IXmlSerializable.

如果你被它冒犯了,你也可以使用其他属性来隐藏这个成员!这不是一个完美的解决方案,但它可以比实现 IXmlSerializable 更快。