Serialization errors in .NET?

I read about serialization and still messed around with BinaryFormatter and SoapFormatter. So far it has been so good - and everything has been fine and serialized, and deserialized.

However, when I try the code below, I would expect that my data file does NOT contain information for Name - and this is so. Why should it contain this when I specify SoapIgnore in a field?

I also tried with SoapAttribute("SomeAttribute") in the Age field, and that didn't make any difference either. The frame version is set to 2.0, but the same thing happens in versions 3.5 and 4.0.

 using System; using System.Runtime.Serialization.Formatters.Soap; using System.IO; using System.Xml.Serialization; class Program { static void Main(string[] args) { Person p = new Person(); p.Age = 42; p.Name = "Mr Smith"; SoapFormatter formatter = new SoapFormatter(); FileStream fs = new FileStream(@"c:\test\data.txt", FileMode.Create); formatter.Serialize(fs, p); } } [Serializable] class Person { public int Age; [SoapIgnore] public string Name; } 
+4
source share
5 answers

Use [NonSerialized] instead of [SoapIgnore]

It is also an older (and aging) API. Wrong, but be sure to read XmlSerialization and ProtoBuf .

And be careful not to mix the API. Serialization is part of the SOAP connection, but not the same. SoapAttribute is not involved in voice serialization.

+9
source

Because serialization and SOAP do not match. Your Name marked as public, so the serializer serializes / deserializes it. If you want to prevent it from appearing in serialization, you must change its protection level to protected.

+1
source

The docs say that the [SoapIgnore] attribute tells the XMLSerializer not to serialize this property, and it does not specify SoapFormatter , so I assume this does not apply to it, although the name suggests that it

+1
source

The following code works. SoapFormatter seems to use the same attributes as the BinaryFormatter, the [NonSerialized] attribute. This, however, contradicts what the MS Press book I am reading says. It displays SoapIgnore, SoapAttribute, and others as attributes that work with SoapFormatter when serializing data.

This code creates two files, none of which have a Name field.

 using System; using System.Runtime.Serialization.Formatters.Soap; using System.IO; using System.Xml.Serialization; class Program { static void Main(string[] args) { Person p = new Person(); p.Age = 42; p.Name = "Mr Smith"; FormatSoap(p); FormatXml(p); } private static void FormatXml(Person p) { XmlSerializer serializer = new XmlSerializer(typeof(Person)); FileStream fs = new FileStream(@"c:\test\xmldata.txt", FileMode.Create); serializer.Serialize(fs, p); } private static void FormatSoap(Person p) { SoapFormatter formatter = new SoapFormatter(); FileStream fs = new FileStream(@"c:\test\soapdata.txt", FileMode.Create); formatter.Serialize(fs, p); } } [Serializable] public class Person { public int Age; [XmlIgnore] [NonSerialized] public string Name; } 
+1
source

All Articles