I needed to serialize an array few weeks ago and i found one way to perform this serialization.

I performed the serialization for the following objects:

_persons.Add(new Employee { FirstName = "John", LastName = "Smith", Age = 32, EmployeeNumber = 213 });
_persons.Add(new Employee { FirstName = "Mike", LastName = "Stuart", Age = 24, EmployeeNumber = 253 });
_persons.Add(new Employee { FirstName = "John", LastName = "Charles", Age = 53, EmployeeNumber = 214 });
_persons.Add(new Employee { FirstName = "Mary", LastName = "Gomez", Age = 37, EmployeeNumber = 234 });
_persons.Add(new Employee { FirstName = "Javier", LastName = "Matias", Age = 33, EmployeeNumber = 453 });

I used the following code to perform this serialization:

Employee[] allEmployeesArray = allEmployees.ToArray();

String objectSerialized = string.Empty;

XmlSerializer serializer = new XmlSerializer(allEmployeesArray.GetType());

MemoryStream memoryStream = new MemoryStream();
memoryStream.Position = 0;
            
using(XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8))
{
    serializer.Serialize(xmlTextWriter, allEmployeesArray);

     //UTF8 ByteArray To String
     memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
     UTF8Encoding encoding = new UTF8Encoding();

     objectSerialized = encoding.GetString(memoryStream.ToArray());
}

memoryStream.Close();
memoryStream.Dispose();

Console.WriteLine(objectSerialized);

And then i had the following result:

LEAVE A REPLY

Please enter your comment!
Please enter your name here