使用char - C#进行数据协定反序列化问题

时间:2012-05-14 10:25:14

标签: c# serialization datacontractserializer xmlserializer

我的代码参考

var sample = DataContractDeserializeObject<MySample>(
                        "<MySample xmlns=\"http://schemas.datacontract.org/2004/07/Sample\"><Charecter>'D'</Charecter></MySample>");

When i run this code. i get this message "There was an error deserializing the object of type MySample. Content cannot be converted to the type Int. Line 1, position 129."

我的反序列化代码

 public static T DataContractDeserializeObject<T>(string objectToDeserialize)
        {
            using (XmlReader reader = XmlReader.Create(new StringReader(objectToDeserialize)))
            {
                var serializer =
                    new DataContractSerializer(typeof(T));
                return (T)serializer.ReadObject(reader);
            }
        }

我的dataContract

[DataContract]
    public class MySample
    {
        [DataMember]
        public char Charecter { get; set; }
    }

如何对char进行反序列化。感谢。

1 个答案:

答案 0 :(得分:5)

我依稀记得有些因为某些原因将字符序列化为ascii表示法。不要试试D,试试104。