How can I prevent a datamember from being serialized

后端 未结 5 1170
鱼传尺愫
鱼传尺愫 2021-01-12 16:58

I only want only to de-serializing a certain data member, without serializing it.

I understand I can set EmitDefaultValue =false, and set the value to null.

<
相关标签:
5条回答
  • 2021-01-12 17:34

    Which serializer?If this is XmlSerializer then either:

    public int Foo {get;set;}
    [XmlIgnore]
    public bool FooSpecified {
        get { return false; } // never serialize
        set { }
    }
    

    or

    public int Foo {get;set;}
    public bool ShouldSerializeFoo() { return false; }
    

    will do this. A quick test shows that this doesn't work for DataContractSerializer, though. protobuf-net also supports both of these, for info.

    0 讨论(0)
  • 2021-01-12 17:42

    Have you tried decorating the property with [IgnoreDataMember]?

    0 讨论(0)
  • 2021-01-12 17:44

    There is the attribute System.Xml.Serialization.XmlIgnoreAttribute wich say to xmkserializers to ignore your property. But it only change xml serialization behavior.

    0 讨论(0)
  • 2021-01-12 17:52

    You can change the value of the data member before the serialization (to the default value, so it doesn't get serialized), but then after the serialization you'd change it back - using the [OnSerializing] and [OnSerialized] callbacks (more information in this blog post). This works fine as long as you don't have multiple threads serializing the object at the same time.

    public class StackOverflow_8010677
    {
        [DataContract(Name = "Person", Namespace = "")]
        public class Person
        {
            [DataMember]
            public string Name;
            [DataMember(EmitDefaultValue = false)]
            public int Age;
    
            private int ageSaved;
            [OnSerializing]
            void OnSerializing(StreamingContext context)
            {
                this.ageSaved = this.Age;
                this.Age = default(int); // will not be serialized
            }
            [OnSerialized]
            void OnSerialized(StreamingContext context)
            {
                this.Age = this.ageSaved;
            }
    
            public override string ToString()
            {
                return string.Format("Person[Name={0},Age={1}]", this.Name, this.Age);
            }
        }
    
        public static void Test()
        {
            Person p1 = new Person { Name = "Jane Roe", Age = 23 };
            MemoryStream ms = new MemoryStream();
            DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
            Console.WriteLine("Serializing: {0}", p1);
            dcs.WriteObject(ms, p1);
            Console.WriteLine("   ==> {0}", Encoding.UTF8.GetString(ms.ToArray()));
            Console.WriteLine("   ==> After serialization: {0}", p1);
            Console.WriteLine();
            Console.WriteLine("Deserializing a XML which contains the Age member");
            const string XML = "<Person><Age>33</Age><Name>John Doe</Name></Person>";
            Person p2 = (Person)dcs.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(XML)));
            Console.WriteLine("  ==> {0}", p2);
        }
    }
    
    0 讨论(0)
  • 2021-01-12 17:53

    add the IgnoreDataMemberAttribute

    0 讨论(0)
提交回复
热议问题