12

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?

Display Name
  • 448
  • 2
  • 5
  • 14
  • What serialiser are you using? The NonSerialized attribute only works with BinaryFormatter or SoapFormatter – Russell Troywest Dec 06 '11 at 09:22
  • I'm using XmlSerializer and BinaryFormatter. I tried adding [XmlIgnore]. Same Problem. Then i tried adding [XmlIgnore] to the property. Works. Strange world... – Display Name Dec 06 '11 at 09:31
  • 1
    Yes, The XmlSerialiser works on public Properties and public fields (if I remember correctly). Whereas the binary serialiser works on private and public fields so it would not have let you use that attribute on a property and the XmlSerialiser would not have let you use the XmlIgnore property on the private field. – Russell Troywest Dec 06 '11 at 09:35
  • You solved problem for XmlSerialization, but what about BinaryFormatter? I have same problem, i use BinaryFormatter but NonSerialized doesn't work. – Vladimir Djurdjevic Oct 24 '15 at 15:47

5 Answers5

24

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).

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
Russell Troywest
  • 8,635
  • 3
  • 35
  • 40
5

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; } }
John
  • 239
  • 1
  • 3
  • 8
  • 1
    A bit late since the post of Russell Troywest has the same answer... 1 year ago ! ;) – JYL Dec 10 '12 at 16:16
2

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.

AlanT
  • 3,627
  • 20
  • 28
  • I could be wrong but I don't think properties get serialized by the BinaryFormatter. Only fields. If they are using a different serialiser that attribute probably wont work. – Russell Troywest Dec 06 '11 at 09:24
  • In fact, the NonSerialized attribute has an attribute of [AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)] so I think adding it to a property would probably give you a compilation error. – Russell Troywest Dec 06 '11 at 09:33
  • My bad, just saw the attribute on the field and went 'Gotcha'. The attribute cannot go on properties and for some reason the XmlSerializer is not honoring it on the field either when I tested it. – AlanT Dec 06 '11 at 13:19
  • OK, better attempt. From http://stackoverflow.com/questions/1296468/suppress-null-value-types-from-being-emitted-by-xmlserializer If you add a method bool ShouldSerializeDecodeText() returning false, it will not serialize the property. – AlanT Dec 06 '11 at 13:28
0

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)#>
<#
        }
0

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; }
}
Triet Nguyen
  • 763
  • 9
  • 20