对于DataContract序列化获取错误:xmlns =''>没想到

时间:2014-08-25 18:36:27

标签: c# serialization

我定义了以下类:

[DataContract(Name = "DataContractTest", Namespace = "")]
public class DataContractTestClass
{
    [DataMember]
    public string TheString { get; set; }

    [DataMember]
    public Guid TheGuid { get; set; }

    [DataMember(Name = "TheNumber")]
    public int TheInteger { get; set; }

    [DataMember]
    public DateTime TheDateTime { get; set; }

    [DataMember]
    public decimal? TheDecimal { get; set; }
}

以下两种方法:

        private static byte[] SerializeContent<T>(T content)
    {
        var formatter = new XmlSerializer(typeof(T));
        using (var memoryStream = new MemoryStream())
        {
            formatter.Serialize(memoryStream, content);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return memoryStream.ToArray();
        }
    }

        private static T DeserializeObject<T>(byte[] contentBytes)
    {
        var xml = Encoding.UTF8.GetString(contentBytes);
        using (var memoryStream = new StringReader(xml))
        {
            var formatter = new XmlSerializer(typeof(T));
            return (T) formatter.Deserialize(memoryStream);
        }
    }

我可以调用SerializeContent()然后使用序列化的结果调用DeserializeContent()但是尝试反序列化时出错。

  

System.InvalidOperationException:DataContractTestClass xmlns =&#39;&#39;没想到。

为什么在尝试反序列化时会出现此错误,而对象只是由C#序列化?

0 个答案:

没有答案