1

Why don't use serialization attribute With XMLSerializer in c#.

http://msdn.microsoft.com/en-us/library/71s92ee1.aspx

Here OrderedItem class is not serialized. What is the reason?

2
  • You are asking why in that example the class OrderedItem doesn't have an attribute that marks it as [Serializable] ? Commented Feb 27, 2013 at 8:55
  • yes, With BinaryFormatter and SoapFormatter we use [Serializable]. Commented Feb 27, 2013 at 8:57

3 Answers 3

2

OrderedItem is very much serialized... I'm guessing you mean "why doesn't it have the [Serializable] attribute?". If so, the answer is simply: the serializer doesn't demand it. It is demanded by BinaryFormatter etc, but they are doing something a bit different - they are serializing the internal state of the object. However, most serializers these days do not check for that; examples:

  • XmlSerializer
  • DataContractSerializer (and all variants)
  • JavascriptSerializer
  • JSON.net
  • protobuf-net
  • and plenty of others

Basically, it just isn't necessary. Perhaps the key difference is that BinaryFormatter could accidentally (without the attribute) end up sending inappropriate data over a remoting boundary, which could cause big problems.

Many "light" frameworks don't even have [Serializable] defined.

Sign up to request clarification or add additional context in comments.

Comments

0

BinaryFormatter uses the [Serializable] attribute. However XMLSerializer doesnt need this. By creating the seralizer

   XmlSerializer serializer = 
   new XmlSerializer(typeof(OrderedItem));

he is assuming that OrderedItem is indeed Serializable.

2 Comments

Is that the reason we put typeof(OrderedItem) ??
@chathura2020 no, that's because XmlSerializer is a contract-based serializer, and without that it wouldn't know what data it is expecting, especially for deserialization. If you don't tell it the type, and then say "deserialize <Foo Bar="123"/> ... what type does it use? You can't just say Foo, because a: there can be multiple Foo, and b: [XmlRoot] can be used to make <Foo.../> the xml for a class called Bar.
0

An explanation i found is that if you mark your class as [Serializable] and serialize that object with a BinaryFormatter for example then even the private members of an instance of that class will be serialized, so it's a mechanism through which you can decide if that class should be serialized in it's entirety or serialize it with an Xml or JS serializer and only get the public members.

So, if you serialize using XmlSerializer only the public members will be serialized.

3 Comments

the "public members" thing is not reliable - that is the case for XmlSerializer, but there are other serializers that will serialize non-public members and do not require [Serializable]. DataContractSerializer for example.
@MarcGravell sure but don't you have to manually mark each member with a [DataMember] attribute in order for the DataContractSerializer to get them?
indeed, there must be some level of elective configuration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.