2

我的代码参考

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]
    public class MySample
    {
        [DataMember]
        public char Charecter { get; set; }
    }

如何反序列化 char。谢谢。

4

1 回答 1

5

我隐约记得由于某种原因将字符序列化为它们的 ascii 表示。不要尝试D,尝试104。

于 2012-05-14T10:30:35.210 回答