Below, I am using XmlSerializer write the object data out to xml file. In the object I have 8 elements worth of data. The serializer is only writing the last element to the file and not all the elements. Do you know why. when I look in the m_file, I see all the data I need. But all the data so not go to the file.
// serialize
XmlSerializer serializer = new XmlSerializer(typeof(DutyCycleTimes));
TextWriter writer = new StreamWriter(fileName);
serializer.Serialize(writer, file, null);
writer.Close();
Loading
Iftikar HussainPosted Jul 5, 2013, 3:45 AM
Try this
SerializeObject(DutyCycleTimes,DutyCycleTimes.GetType())
public string SerializeObject(object obj, Type type)
{
int count;
byte[] byteArray;
char[] charArray;
System.Text.UTF8Encoding encoding = new UTF8Encoding();
XmlSerializer xs = new XmlSerializer(type);
using (MemoryStream memStream = new MemoryStream())
{
xs.Serialize(memStream, obj);
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the all bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, (int)memStream.Length);
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[encoding.GetCharCount(byteArray, 0, count)];
encoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
}
return new string(charArray);
}
Regards,Iftikar
David SmithPosted Jul 5, 2013, 4:00 AM