How to Ignore a Property When Serializing Using the Datacontractserializer

How can I ignore a property when serializing using the DataContractSerializer?

You might be looking for IgnoreDataMemberAttribute.

How can I ignore a property with the DataContractSerializer (can't delete DataMember)?

You can remove the DataMember attribute and keep your property like this :

public string Password { get; set; }

If your classe is decored with [DataContract] the DataContractSerializer will serialize all public properties decorated with DataMember attribute, and if your class is not decorated you can use [IgnoreDataMember] attribute.

EDIT

Maybe you can try with a custom surrogate I dont know if its the good way for doing this.

class Program
{
static void Main(string[] args)
{
var s = Serialize(new BackgroundJobInfo() { Password = "toto", Text = "text" });
var myJob = Deserialize(s);
}

public static string Serialize(BackgroundJobInfo info)
{
MySurrogate mySurrogate = new MySurrogate();
DataContractSerializer dataContractSerializer =
new DataContractSerializer(
typeof(BackgroundJobInfo),
null,
64 * 1024,
true,
true,
mySurrogate);

var stringBuilder = new StringBuilder();

using (var stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
{
var writer = new XmlTextWriter(stringWriter);
dataContractSerializer.WriteObject(writer, info);
}

return stringBuilder.ToString();
}

public static BackgroundJobInfo Deserialize(string info)
{
var dataContractSerializer = new DataContractSerializer(typeof(BackgroundJobInfo));
using (var xmlTextReader = new XmlTextReader(info, XmlNodeType.Document, new XmlParserContext(null, null, null, XmlSpace.None)))
{
try
{
var result = (BackgroundJobInfo)dataContractSerializer.ReadObject(xmlTextReader);
return result;
}
catch (Exception e)
{
return null;
}
}
}
}

internal class MySurrogate : IDataContractSurrogate
{
public Type GetDataContractType(Type type)
{
return typeof (BackgroundJobInfo);
}

public object GetObjectToSerialize(object obj, Type targetType)
{
var maskedMembers = obj.GetType().GetProperties().Where(
m => m.GetCustomAttributes(typeof(DataMemberAttribute), true).Any()
&& m.GetCustomAttributes(typeof(DoNotSerializeAttribute), true).Any());
foreach (var member in maskedMembers)
{
member.SetValue(obj, null, null);
}
return obj;
}

public object GetDeserializedObject(object obj, Type targetType)
{
throw new NotImplementedException();
}

public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
{
throw new NotImplementedException();
}

public object GetCustomDataToExport(Type clrType, Type dataContractType)
{
throw new NotImplementedException();
}

public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
{
throw new NotImplementedException();
}

public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
{
throw new NotImplementedException();
}

public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
{
throw new NotImplementedException();
}
}

internal class DoNotSerializeAttribute : Attribute
{
}

[DataContract]
public class BackgroundJobInfo
{
[DataMember(Name = "password")]
[DoNotSerializeAttribute]
public string Password { get; set; }

[DataMember(Name = "text")]
public string Text { get; set; }
}

Sample Image

How to conditionally avoid property from serializing in WCF?

If I've understood your requirement, the following should work. The property ValueIfFlagTrue is for serialization only, name it as you wish. You haven't said how you want to handle deserializing, so I've made a guess in the implementation: adjust as needed.

[DataContract]
public class CompositeType
{
public bool _flag = true;

public decimal? Value { get; set; }

[DataMember(Name = "Value", EmitDefaultValue = false)]
private decimal? ValueIfFlagTrue
{
get
{
return _flag ? Value : null;
}
set
{
_flag = value.HasValue ? true : false;
Value = value;
}
}
}

UPDATED

You can put the DataMember attribute on a private property, so there's no need to expose the ValueIfFlagTrue property publicly.

Ignore field order in DataContractSerializer

No it's not optional. It's optional to explicitly specify an ordering, but otherwise the order is determined by the basic rules you linked to (base types first, alphabetic, ...).

The introductory paragraph is simply implying that you don't always need to know or care about the order - for example if you are using a generated proxy.

If you're using DataContractSerializer to deserialize a file, the best solution I can think of would be to use XSLT to transform it into the correct order before deserializing.

If you're calling a web service (what DataContractSerializer was designed for), you're better off sticking to the order in the contract.

Data Contract Serializer - How to omit the outer element of a collection

The DataContract serializer does not allow this degree of control over the resulted XML, you will have to use instead the XmlSerializer in order to achieve this.



Related Topics



Leave a reply



Submit