6

I have a VB.NET class which I'm serializing via XML in an asmx file. I've added attributes to the datamember I want to ignore in serialization, but it's still returned. I also have the <DataContract()> attribute on my class and the DataMember attribute on all properties which should be serialized. My property declaration is:

    <ScriptIgnore()> _
    <IgnoreDataMember()> _
    Public Property Address() As SomeObject
Echilon
  • 10,064
  • 33
  • 131
  • 217

2 Answers2

10

By adding an attribute to the backing field and converting it from an auto-property, I eventually got the proprty to stop serializing:

<NonSerialized()> _
Private _address As SomeObject = Nothing
<ScriptIgnore()> _
<IgnoreDataMember()> _
<Xmlignore()>
Public Property address() As SomeObject
    Get
        Return _address
    End Get
    Set(ByVal value As SomeObject)
        _address = value
    End Set
End Property
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Echilon
  • 10,064
  • 33
  • 131
  • 217
3

Have you tried the NonSerialized attribute:

<NonSerialized()> _
Public Property Address() As SomeObject

http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

Jay
  • 5,897
  • 1
  • 25
  • 28
  • Unfortunately, this gives: Attribute 'NonSerializedAttribute' cannot be applied to 'Address' because the attribute is not valid on this declaration type. – Echilon Jul 19 '11 at 13:05
  • You will need to change you property to not be auto-implemented to make it work, and put the attribute on the backing store. I didn't realize that was the case. Here is a link to an question that explains it. http://stackoverflow.com/questions/1728367/how-to-prevent-auto-implemented-properties-from-being-serialized – Jay Jul 20 '11 at 14:04