使用XmlSerializer序列化char数据类型

Adi*_*dov 10 c# xml-serialization

我有一个类具有属性whiches类型是char如下

    [XmlRoot("Root")]
    public class TestClass
    {
        [XmlElement("Test", typeof(char))]
        public char TestProperty { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

当TestProperty的值为'N'并且如果我序列化TestClass,它将产生以下结果:

    <Root>
        <Test>78</Test>
    </Root>
Run Code Online (Sandbox Code Playgroud)

但我想要的是跟随

    <Root>
        <Test>N</Test>
    </Root>
Run Code Online (Sandbox Code Playgroud)

是否可以在不将TestProperty的类型更改为字符串的情况下?

Mar*_*ell 8

不是AFAIK.但你可以作弊:

[XmlIgnore]
public char TestProperty { get; set; }

[XmlElement("Test"), Browsable(false)]
public string TestPropertyString {
    get { return TestProperty.ToString(); }
    set { TestProperty = value.Single(); }
}
Run Code Online (Sandbox Code Playgroud)