Peter Olsen
Peter Olsen

Reputation: 59

Serialization bugs in .NET?

I'm reading up on serialization, and have so far been messing with BinaryFormatter and SoapFormatter. So far, so good - and everything have both Serialized and Deserialized perfectly.

However, when I try the code below, I would expect my datafile to NOT contains the info for Name - and it does. Why would it contain that, when I specify SoapIgnore on the field?

I also tried with SoapAttribute("SomeAttribute") on the Age-field, and that didn't make any difference either. The framework version is set to 2.0, but the same thing happens in 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;
}

Upvotes: 4

Views: 1174

Answers (5)

Peter Olsen
Peter Olsen

Reputation: 59

The following code works. It seems that the SoapFormatter uses the same attributes as the BinaryFormatter does - the [NonSerialized] attribute. This however goes against what the MS Press book I'm reading states. It lists SoapIgnore, SoapAttribute and others as attributes that works with the SoapFormatter when serializing data.

This code creates two files, none of them having the 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;
}

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273581

Use [NonSerialized] instead of [SoapIgnore]

Furthermore, this is an older (and aging) API. Not directly wrong but be sure to read up on XmlSerialization and ProtoBuf as well.

And be careful not to mix the API's up. Serialization is part of SOAP communication but not the same. SoapAttribute is not involved in bare Serialization.

Upvotes: 10

Iain Ward
Iain Ward

Reputation: 9936

From the docs it states that the [SoapIgnore] attribute tells the XMLSerializer not to serialize this property, and it doesn't mention the SoapFormatter so I assume it doesn't apply to it, even though the name suggests it does

Upvotes: 1

Joel Etherton
Joel Etherton

Reputation: 37543

Because serialization and SOAP aren't the same. You have Name marked as public, so the serializer will serialize/deserialize it. If you want it not to appear in a serialization you should change its protection level to protected.

Upvotes: 1

Related Questions