WCF Datacontract, some fields do not deserialize

北城余情 提交于 2019-11-26 17:53:59

Fields are in the wrong order for your message. DataContracts default to Alphabetical ordering and not order of declaration; and expects XML elements to arrive in that order; Out of order elements are discarded usually.

Either fix your contract to specify the right order explicitly (using the Order property of the DataMemberAttribute) or make sure your client sends them in the right one.

You can try to use XmlSerializer instead of DataContractSerializer. In my case, I need to change default engine in global.asax file:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

Do this carefully because some XML can become not valid, for example - namespaces, with XmlSerializer should be determined like:

[XmlNamespaceDeclarations] 
private XmlSerializerNamespaces xmlns 
{  
  get {
    var xns = new XmlSerializerNamespaces();
    xns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");
    return xns;
  }    
  set { } 
}

Or u can set XmlSerializerFormatAtrribute to You class (not work for me). Look in url head "Manually Switching to the XmlSerializer"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!