1

Possible Duplicate:
When should i use [Serializable] in C#?

How is it important to use [Serializable()] in the beginning of class (after namespace and before class) when we are dealing with serialization?

I'm looking at a sample as following:

namespace MyObjSerial
{
    [Serializable()]    //Set this attribute to all the classes that you define to be serialized
    public class Employee : ISerializable
    {
        public int EmpId;
        public string EmpName;

        //Default constructor
        public Employee()
        {
            EmpId = 0;
            EmpName = null;
        }
    }
}
Community
  • 1
  • 1
amit kohan
  • 1,612
  • 2
  • 25
  • 47
  • 3
    http://stackoverflow.com/a/5877839/1069200 – Johan Larsson Oct 30 '12 at 17:43
  • This question has XML and XAML tags; how are they related to the question? For XML, if it's because you're serializing to XML, please edit your question to include that fact (and it would make the question different from that possible duplicate). For XAML, I cannot imagine how it applies. –  Oct 30 '12 at 17:50

1 Answers1

1

Binary Serialization[highly automated]

Two ways to make type Binary Serializable

{if you want to serialize in XML this is not what you want}

implementing ISerializable gives you total control over the serialization

OR

Use attributes like serialzable,nonSerialized and various other attributes. Using serialzable attribute instructs the serializer to include all fields in the type including private,publicbut notproperties`


XML Serialization[less automated]

I guess you want to serialize the class in XML,in that case

Two ways to make type XML Serializable

use System.Xml.Serialization attributes

OR

implement IXmlSerializer for more control


Your Question

is it important to use [Serializable()] in the beginning of class (after namespace and before class) when we are dealing with serialization

Ofcorse..This is how you are going to tell the compiler to serialize those particular types.But you should use the attributes or interface according to what the class should serialize to.

So,using [Serializable()] attribute for serializing the type to XML would not work.It is used for Binary Serialization.You should sprinkle attributes of System.Xml.Serialization attributes over the class or use IXmlSerializer for XML-serialization

You can use

1>Data Contract Serializer
2>Binary Serializer
3>Xml Serializer
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Minor pedantic point: "binary serialization" is a generic term, and includes a range of serializers. Your notes apply only to BinaryFormatter, which is just *one* binary serializer. – Marc Gravell Oct 30 '12 at 18:12
  • The XML Serializer does not use the `[Serializable]` attribute. – John Saunders Oct 30 '12 at 18:21
  • @JohnSaunders i never said that..i said `System.Xml.Serialization` attributes & `IXmlSerializer` – Anirudha Oct 30 '12 at 18:23
  • You also said, "Ofcorse..This is how you are going to tell the compiler to serialize those particular types." – John Saunders Oct 30 '12 at 18:25
  • @JohnSaunders ahh...corrected it..thx to point it out – Anirudha Oct 30 '12 at 18:29
  • Correct me if I am wrong ... _"using [Serializable()] attribute for serializing the type to XML would not work.It is used for Binary Serialization"_ does it mean I should not use it for serializing in xml? – amit kohan Oct 30 '12 at 18:33
  • @amitkohan yes `[Serializable()]` is not used for xml serialization..see this [tutorial](http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization) – Anirudha Oct 30 '12 at 18:37