It depends on the implementation of the serializer you are using to serialize the object.
If you try this, you will get what you are expecting:
using System.IO;
using System.Numerics;
using System.Runtime.Serialization.Formatters.Soap;
public class Test {
public static void Main() {
var c = new Complex(1, 2);
Stream stream = File.Open("data.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, c);
stream.Close();
}
}
Instead, if you use classes in the System.Xml.Serialization
namespace, you will get something similar to what you posted:
using System;
using System.IO;
using System.Numerics;
using System.Xml.Serialization;
public class Test {
public static void Main() {
var c = new Complex(1, 2);
XmlSerializer s = new XmlSerializer(typeof(Complex));
StringWriter sw = new StringWriter();
s.Serialize(sw, c);
Console.WriteLine(sw.ToString());
}
}
I think that this is due to the fact that the XmlSerializer will not serialize private members (as are m_real
and m_imaginary
in the Complex
struct).