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?
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?
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:
XmlSerializerDataContractSerializer (and all variants)JavascriptSerializerBasically, 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.
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.
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.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.
XmlSerializer, but there are other serializers that will serialize non-public members and do not require [Serializable]. DataContractSerializer for example.