【问题标题】:Serialize char data type with XmlSerializer使用 XmlSerializer 序列化 char 数据类型
【发布时间】:2013-04-17 07:28:32
【问题描述】:

我有一个类,其类型为 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 的类型更改为字符串?

【问题讨论】:

    标签: c# xml-serialization


    【解决方案1】:

    不是AFAIK。不过,你可以作弊:

    [XmlIgnore]
    public char TestProperty { get; set; }
    
    [XmlElement("Test"), Browsable(false)]
    public string TestPropertyString {
        get { return TestProperty.ToString(); }
        set { TestProperty = value.Single(); }
    }
    

    【讨论】:

    • 谢谢。我现在不会尝试实现这一点很好)。那么我认为最好将 TestProperty 的类型更改为字符串。
    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多