SerializableAttribute?

J

Julie

Hi everyone,
I've been looking over serialization and am slightly confused... Is the
SerializableAttribute required above the class when using XML serialization?

If not, why not? If so, why so?

Thanks in advance.

Example:
[Serializable()] // REQUIRED??!?!
public class Group
{
[XmlAttribute ()]
public string GroupName;

[XmlAttribute()]
public Byte [] GroupNumber;

[XmlAttribute()]
public DateTime Today;
}
 
A

Alberto Poblacion

Julie said:
I've been looking over serialization and am slightly confused... Is the
SerializableAttribute required above the class when using XML
serialization?

No. Only runtime serialization (binary or SOAP) requires the
SerializableAttribute. The XmlSerializer does not need it.
If not, why not? If so, why so?

The immediate reason is that when runtime serialization performs
reflection on the serialized object to extract it members, it first checks
to see if the attribute is present, and otherwise refuses to proceed. The
XmlSerializer does not perform any such checks. However, this leads us to
question why the designers of the Framework class libraries decided to make
things in that way. I am not aware of any document explainig the reasoning
that led to this design.
 
T

Tony Johansson

Julie said:
Hi everyone,
I've been looking over serialization and am slightly confused... Is the
SerializableAttribute required above the class when using XML
serialization?

If not, why not? If so, why so?

Thanks in advance.

Example:
[Serializable()] // REQUIRED??!?!
public class Group
{
[XmlAttribute ()]
public string GroupName;

[XmlAttribute()]
public Byte [] GroupNumber;

[XmlAttribute()]
public DateTime Today;
}

If you will use XML Serialization you must perform the following task.
1.Specify the class as public
2.Specify all members that must be serialized as public
3.Create a parameterless c-tor. Often called default c-tor

Note as you mentioned unlike classes processed with standard serialization,
classes do not have to have the serialization attribute to be processed with
the XML serialization, If there are private or protected members, they will
be skipped during serialization.

If you want to know why you don't have to specify serialization attribute
you have to ask the person who created .NET!

//Tony
 
M

Mr. Arnold

Julie said:
Hi everyone,
I've been looking over serialization and am slightly confused... Is the
SerializableAttribute required above the class when using XML
serialization?

If not, why not? If so, why so?

Thanks in advance.

Example:
[Serializable()] // REQUIRED??!?!
public class Group
{
[XmlAttribute ()]
public string GroupName;

[XmlAttribute()]
public Byte [] GroupNumber;

[XmlAttribute()]
public DateTime Today;
}

The attribute is required if you want to XML serialize the class/object.
I don't know what the XMLAttribute is about. I don't think you need it.

Where is the get/set for each one of the public properties? Maybe I am
missing something here, but you do need to get data from the property
and set data in the property with backing private variables in the class
for the public properties.

Or you can use auto-property.
 
A

Arne Vajhøj

I've been looking over serialization and am slightly confused... Is the
SerializableAttribute required above the class when using XML
serialization?
No.

If not, why not? If so, why so?

Real serialization (binary serialization) and XML serialization
actually works very differently.

Look at the following program:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace E
{
[Serializable]
public class Data
{
private int v1;
private int v2;
public Data()
{
v1 = 1;
v2 = 2;
}
public void IncV1()
{
v1++;
}
public int V2 { get { return v2; } set { v2 = value; } }
public override string ToString()
{
return "(" + v1 + "," + v2 + ")";
}
}
public class Program
{
public static void Main(string[] args)
{
Data o = new Data();
o.IncV1();
o.V2 = 3;
Console.WriteLine(o);
XmlSerializer ser = new XmlSerializer(typeof(Data));
StreamWriter sw = new StreamWriter(@"C:\data.xml");
ser.Serialize(sw, o);
sw.Close();
StreamReader sr = new StreamReader(@"C:\data.xml");
Data ox = (Data)ser.Deserialize(sr);
sr.Close();
Console.WriteLine(ox);
BinaryFormatter bf = new BinaryFormatter();
FileStream outf = File.Create(@"C:\data.bin");
bf.Serialize(outf, o);
outf.Close();
FileStream inf = File.OpenRead(@"C:\data.bin");
Data ob = (Data)bf.Deserialize(inf);
inf.Close();
Console.WriteLine(ob);
Console.ReadKey();
}
}
}

It outputs:

(2,3)
(1,3)
(2,3)

The real/binary serialization is a feature built deep into the CLR. It
needs the attribute as verification that it can do its job.

The XML serialization is a mucg higher level thing. I assume that
it uses simple reflection. It does not need the attribute. Public
properties is a good enough verification.

BTW, it is the same thing in Java.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top