我有一个类,其属性类型为 char,如下所示
[XmlRoot("Root")]
public class TestClass
{
[XmlElement("Test", typeof(char))]
public char TestProperty { get; set; }
}
当 TestProperty 的值为“N”时,如果我序列化 TestClass,它将产生以下结果:
<Root>
<Test>78</Test>
</Root>
但我想要的是有跟随
<Root>
<Test>N</Test>
</Root>
是否可以不将 TestProperty 的类型更改为字符串?
最佳答案
不是 AFAIK。不过你可以作弊:
[XmlIgnore]
public char TestProperty { get; set; }
[XmlElement("Test"), Browsable(false)]
public string TestPropertyString {
get { return TestProperty.ToString(); }
set { TestProperty = value.Single(); }
}
关于c# - 使用 XmlSerializer 序列化 char 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16054090/