You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to develop an application that uses the Entity Framework.
Entity types in the model are generated by the Entity Data Model generator tool (EdmGen.exe).
You write the following code. (Line numbers are included for reference only.)
01 MemoryStream stream = new MemoryStream();
02 var query = context.Contacts.Include(SalesOrderHeaders.SalesOrderDetails);
03 var contact = query.Where(it.LastName = @lastname, new ObjectParameter(lastname, lastName)).First();
04 ….
You need to serialize the contact and all of its related objects to the MemoryStream so that the contact can be deserialized back into the model.
Which code segment should you insert at line 04?
A.
var formatter = new XmlSerializer(typeof(Contact), new Type[] {
typeof(SalesOrderHeader),
typeof(SalesOrderDetail)
});
formatter.Serialize(stream, contact);
B.
var formatter = new XmlSerializer(typeof(Contact));
formatter.Serialize(stream, contact);
C.
var formatter = new BinaryFormatter();
formatter.Serialize(stream, contact);
D.
var formatter = new SoapFormatter();
formatter.Serialize(stream, contact);
Explanation:
public XmlSerializer(Type type, Type[] extraTypes)
Initializes a new instance of the System.Xml.Serialization.XmlSerializer class that can serialize objects of the specified type into XML documents,
and deserialize XML documents into object of a specified type. If a property or field returns an array, the extraTypes parameter specifies objects
that can be inserted into the array.
type:
The type of the object that this System.Xml.Serialization.XmlSerializer can serialize.
extraTypes:
A System.Type array of additional object types to serialize.XmlSerializer Constructor (Type, Type[])
(http://msdn.microsoft.com/en-us/library/e5aakyae.aspx)
Answer is C . Binaryformattter().
XMLSerializer can not serialize related objects
You are correct. You need to use BinaryFormatter(). See the following article:
http://eskurikhin.blogspot.com/2012/11/serialization-of-entities-entity.html
I’m not sure that it can be deserialized with BinaryFormatter as well. Especially with SalesOrderHeader and SalesOrderDetail included. But anyways C looks correct for me too.