繁体   English   中英

更改DataContract的名称空间

[英]Change the namespace of DataContract

我必须从其他Windows服务保存的数据库中检索xml数据。

这里的XML数据存储在我的数据库中:

<ArrayOfDescription.Traduction xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/OtherNamespace">
   <Description.Traduction>
      <Description>Contact ouvert</Description>
      <Label>Ouvert</Label>
      <LcId>12</LcId>
   </Description.Traduction>
   <Description.Traduction>
      <Description>Contact open</Description>
      <Label>Open</Label>
      <LcId>9</LcId>
   </Description.Traduction>
</ArrayOfDescription.Traduction>

我的类名称是Description,我的字符串属性名称是TradBlob。 检索存储在我的数据库中的数据没有问题。 然后,我定义了Description的局部类来帮助我进行反序列化。

public partial class Description
{
     [DataContract(Name = "Description.Traduction", Namespace = "http://schemas.datacontract.org/2004/07/OtherNamespace")]
     public class Traduction
     {
        public string Description { get; set; }
        public string Label { get; set; }
        public int LcId { get; set; }
     }
}

然后在UI端,我可以这样写:

LabelTrad = SerializerHelper.Deserialize<List<Description.Traduction>>(TradBlob).Single(x=>x.LcId == CurrentLcId).Label

这是我的反序列化方法:

public static T Deserialize<T>(string xml)
{
  if (string.IsNullOrEmpty(xml)) return default(T);

  try
  {
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
    {
      var serializer = new DataContractSerializer(typeof(T));
      T theObject = (T)serializer.ReadObject(stream);
      return theObject;
    }
  }
  catch (SerializationException e)
  {
    // La déserialisation s'est mal passée, on retourne null
    return default(T);
  }
}

我的问题是反序列化无法按预期进行。 标签为空。

您有什么问题的想法吗?

注意:我无法在Description类中修改Traduction类的创建。

我忘记将DataMember批注添加到Description,Label和LcId属性。 谢谢大家

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM