0

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 an XML file and don't want to save "Data" property. How I do this?

kaya3
  • 47,440
  • 4
  • 68
  • 97
xtmq
  • 3,380
  • 22
  • 26

2 Answers2

0

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

lehn0058
  • 19,977
  • 15
  • 69
  • 109
0

Make Data optional:

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

Then erase Data:

result.Data = null;
Cœur
  • 37,241
  • 25
  • 195
  • 267