Pergunta

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!

Foi útil?

Solução

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

Outras dicas

Make Data optional:

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

Then erase Data:

result.Data = null;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top