Display Name
Display Name

Reputation: 458

Prevent Property from being serialized

I tried something like this:

    [NonSerialized]
    private string _DecodeText;
    public string DecodeText { get { return _DecodeText; } set { _DecodeText = value; } }

But it does not work. "DecodeText" is still in the serialized file. How can i prevent the property from serializing?

Upvotes: 12

Views: 31998

Answers (5)

Triet Nguyen
Triet Nguyen

Reputation: 821

I think this code that will be help you all. With properties you declared and you want it to be serialized only. Then you should add a method return type as boolean and name method is ShouldSerialize as prefix with [NameProperty]. A scratch code as below and link reference to Newtonsoft for you:

public class DisplayFieldSetting
{
        public bool ShouldSerializeHidden()
        {
            return false;
        }

        public bool ShouldSerializeKeepOriginialColumnName()
        {
            return false;
        }


        public string Hidden { get; set; }
        public string KeepOriginialColumnName{ get; set; }
}

Upvotes: 0

Michael Goldshmidt
Michael Goldshmidt

Reputation: 141

I built on top of @John's answer and modified ef.tt template to include [System.Xml.Serialization.XmlIgnore]

Here is the code

        foreach (var navigationProperty in navigationProperties)
        {
            if (navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            {
#>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    [System.Xml.Serialization.XmlIgnore]
<#
            }
#>
    <#=codeStringGenerator.NavigationProperty(navigationProperty)#>
<#
        }

Upvotes: 0

Russell Troywest
Russell Troywest

Reputation: 8776

I Suspect you're using the XmlSerializer? If so use the [XmlIgnore] attribute instead.

This should be applied to the property instead of the backing field as the XmlSerializer serializes public fields and properties (whereas the BinaryFormatter uses refelction to get at the private fields - hence the marking of the private field with NonSerialized when using a BinaryFormatter).

Upvotes: 24

John
John

Reputation: 239

I was able to use the following and not have the property serialized (.NET 4.0):

private string _DecodeText;
[System.Xml.Serialization.XmlIgnore]
public string DecodeText { get { return _DecodeText; } set { _DecodeText = value; } }

Upvotes: 5

AlanT
AlanT

Reputation: 3663

Updated Answer

The [NonSerialized] atttibute is on the variable not the property, but it cannot be on the attribute. So it is not going to help.

One way to prevent the property being serialized is to add a method

public bool ShouldSerializeDecodeText() {
   return false;
}

and this (for the XmlSerializer at least) will prevent the property being serialized.

If you don't want to add lots of methods to the class just for serialization you might try inheriting from it and adding the methods to the derived class.

hth, Alan.

Upvotes: 2

Related Questions