I have a class:

[DataContract]
public class Result
{
  [DataMember] public String Log {get; set;}
  [DataMember] public String Data {get; set;}
}

I send this class via WCF and it works fine. But I want to save this class to a xml file after receiving. I try to write:

var serializer = new DataContractSerializer(typeof (T),null,int.MaxValue,
    false,true,null,new SharedTypeXmlResolver());
var settings = new XmlWriterSettings { Indent = true };
using (var backing = new StringWriter(CultureInfo.InvariantCulture))
{
    using (var writer = XmlWriter.Create(backing, settings))
    {
        serializer.WriteObject(writer, result);                                        
    }
    return backing.ToString();
} 

But I want to save "Log" property to a xml file and don't want to save "Data" property. How I do this? Thanks!

有帮助吗?

解决方案

XmlIgnore does not work with DataContract serialization. This contract is opt-in, as opposed to Serializable being opt-out.

其他提示

Make Data optional:

[DataMember(EmitDefaultValue = false)] public String Data {get; set;}

Then erase Data:

result.Data = null;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top