如何告诉XmlSerializer始终使用[DefautValue(...)]序列化属性? [英] How to tell XmlSerializer to serialize properties with [DefautValue(...)] always?
问题描述
我将 DefaultValue
属性用于正确的 PropertyGrid
行为(它以粗体显示与默认值不同的值)。现在,如果我想使用 XmlSerializer
序列化显示的对象,则在xml文件中没有具有默认值的属性的条目。
I am using DefaultValue
attribute for the proper PropertyGrid
behavior (it shows values different from default in bold). Now if I want to serialize shown object with the use of XmlSerializer
there will be no entries in xml-file for properties with default values.
告诉XmlSerializer仍将这些序列化的最简单方法是什么?
我需要它来支持版本,因此,当我稍后在代码中更改默认值时-序列化属性将获取其序列化的值,而不是最新的值。我可以考虑以下问题:
I need that to support "versions", so when I change default value later in the code - serialized property gets value it had serialized with, not "latest" one. I can think about following:
- 覆盖
PropertyGrid
的行为(使用自定义属性,因此它会被XmlSerializer
)忽略; - 执行某种自定义xml序列化操作,其中忽略
DefaultValue
's; - 在将对象传递给
XmlSeriazer
之前先对它做一些事情,这样它就不会包含DefaultValue
了。
- Override behavior of
PropertyGrid
(use custom attribute, so it will be ignoreed byXmlSerializer
); - Do sort of a custom xml-serialization, where ignore
DefaultValue
's; - Do something with object before passing it to
XmlSeriazer
so it won't containDefaultValue
's anymore.
但是我有机会错过一些秘密属性,可以轻松完成此操作= D。
But there is a chance I miss some secret property what allows to do it without much pain =D.
以下是我想要的示例:
private bool _allowNegative = false;
/// <summary>
/// Get or set if negative results are allowed
/// </summary>
[Category(CategoryAnalyse)]
[Admin]
[TypeConverter(typeof(ConverterBoolOnOff))]
//[DefaultValue(false)] *1
public bool AllowNegative
{
get { return _allowNegative; }
set
{
_allowNegative = value;
ConfigBase.OnConfigChanged();
}
}
//public void ResetAllowNegative() { _allowNegative = false; } *2
//public bool ShouldSerializeAllowNegative() { return _allowNegative; } *3
//public bool ShouldSerializeAllowNegative() { return true; } *4
如果我取消注释(* 1),则在 PropertyGrid
-具有默认值的属性以普通文本显示,否则文本为粗体。但是 XmlSerializer
会不将具有默认值的属性放入xml文件中,这是 Bad (我正在尝试
If I uncomment (*1), then I have desired effect in PropertyGrid
- properties with default values are displayed in normal text, otherwise text is bold. However XmlSerializer
will NOT put properties with default value into xml-file and this is BAD (and I am trying to fix it).
如果我取消注释(* 2)和(* 3),则与取消注释(* 1)完全相同。
If I uncomment (*2) and (*3), then it's totally same as uncommenting (*1).
如果我取消注释(* 2)和(* 4),那么 XmlSerializer
总是将属性放入xml文件,但这是因为它们不再具有默认值,并且 PropertyGrid
以粗体显示所有值。
If I uncomment (*2) and (*4), then XmlSerializer
will always put properties into xml-file, but this happens because they do not have default value anymore and PropertyGrid
shows all values in bold text.
推荐答案
As long as you don't need attributes in your Xml, if you use the DataContractSerializer instead you will get the behavior you desire.
[DataContract]
public class Test
{
[DataMember]
[DefaultValue(false)]
public bool AllowNegative { get; set; }
}
void Main()
{
var sb2 = new StringBuilder();
var dcs = new DataContractSerializer(typeof(Test));
using(var writer = XmlWriter.Create(sb2))
{
dcs.WriteObject(writer, new Test());
}
Console.WriteLine(sb2.ToString());
}
产生(减去名称空间等)
produces (minus namespaces etc)
<Test>
<AllowNegative>false</AllowNegative>
</Test>
这篇关于如何告诉XmlSerializer始终使用[DefautValue(...)]序列化属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!