// Serializing an object MyObject obj = new MyObject(); // code to populate obj with data BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, obj); // Deserializing the object stream.Position = 0; SerializationInfo info = formatter.Deserialize(stream) as SerializationInfo; // Retrieving the byte value of a data member byte value = info.GetByte("myDataMember");
// Serializing an object MyObject obj = new MyObject(); // code to populate obj with data XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); MemoryStream stream = new MemoryStream(); serializer.Serialize(stream, obj); // Deserializing the object stream.Position = 0; XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlElement root = doc.DocumentElement; SerializationInfo info = new SerializationInfo(typeof(MyObject), new FormatterConverter()); // Retrieving the byte value of a data member byte value = info.GetByte("myDataMember");In this example, we first serialize an object of the MyObject class using the XmlSerializer. We then deserialize the object and store its data in an XmlDocument object. From the XmlDocument, we retrieve the root element and create a new SerializationInfo object. Finally, we use the GetByte method to retrieve the byte value of a specific data member named "myDataMember" from the SerializationInfo object. Package/Library: System.Runtime.Serialization.Formatters.Binary or System.Xml.Serialization