gpt4 book ai didi

c# - XmlSerializer 定义默认值

转载 作者:太空狗 更新时间:2023-10-30 00:12:09 24 4
gpt4 key购买 nike

我们有一个包含一些用户设置的 xml 文档。我们刚刚添加了一个新设置(在遗留 xml 文档中找不到)并且 XmlSerializer 自动将其设置为 false。我尝试了 DefaultValueAttribute 但它不起作用。关于如何使默认值为 true 的任何想法?这是代码:

private bool _property = true;
[DefaultValueAttribute(true)]
public bool Property
{
get { return _property; }
set
{
if (_property != value)
{
_property = value;
this.IsModified = true;
}
}
}

谢谢!

最佳答案

DefaultValue 影响序列化,就好像在运行时属性具有与 DefaultValue 所说的值相匹配的值,然后 XmlSerializer 实际上不会写出该元素(因为它是默认值)。

我不确定它是否会影响读取时的默认值,但在快速测试中似乎不会。在您的场景中,我可能只是将其设为一个带有支持字段的属性,并带有一个字段初始值设定项,使其成为“真”。恕我直言,与 ctor 方法相比,我更喜欢这种方法,因为它将它与您为该类定义或未定义的 ctors 分离,但这是相同的目标。

using System;
using System.ComponentModel;
using System.Xml.Serialization;
using System.IO;

class Program
{
static void Main(string[] args)
{
var serializer = new XmlSerializer(typeof(Testing));

string serializedString;
Testing instance = new Testing();
using (StringWriter writer = new StringWriter())
{
instance.SomeProperty = true;
serializer.Serialize(writer, instance);
serializedString = writer.ToString();
}
Console.WriteLine("Serialized instance with SomeProperty={0} out as {1}", instance.SomeProperty, serializedString);
using (StringReader reader = new StringReader(serializedString))
{
instance = (Testing)serializer.Deserialize(reader);
Console.WriteLine("Deserialized string {0} into instance with SomeProperty={1}", serializedString, instance.SomeProperty);
}
Console.ReadLine();
}
}

public class Testing
{
[DefaultValue(true)]
public bool SomeProperty { get; set; }
}

正如我在评论中提到的,关于 xml 序列化属性的页面 (http://msdn.microsoft.com/en-us/library/83y7df3e.aspx) 声称 DefaultValue 确实会使序列化程序设置值当它丢失时,但它在这个测试代码中没有这样做(类似于上面,但只是反序列化 3 个输入)。

using System;
using System.ComponentModel;
using System.Xml.Serialization;
using System.IO;

class Program
{
private static string[] s_inputs = new[]
{
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Testing xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" />",

@"<?xml version=""1.0"" encoding=""utf-16""?>
<Testing xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<SomeProperty />
</Testing>",

@"<?xml version=""1.0"" encoding=""utf-16""?>
<Testing xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<SomeProperty>true</SomeProperty>
</Testing>",
};

static void Main(string[] args)
{
var serializer = new XmlSerializer(typeof(Testing));

foreach (var input in s_inputs)
{
using (StringReader reader = new StringReader(input))
{
Testing instance = (Testing)serializer.Deserialize(reader);
Console.WriteLine("Deserialized string \n{0}\n into instance with SomeProperty={1}", input, instance.SomeProperty);
}
}
Console.ReadLine();
}
}

public class Testing
{
[DefaultValue(true)]
public bool SomeProperty { get; set; }
}

关于c# - XmlSerializer 定义默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7290618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com