How to serialize "System.Numerics.Complex" using XmlSerializer?

This is the XML result that I get when the object is Complex[]serialized:

<MyClass>               
    <Complex />
    <Complex />
    <Complex />
    <Complex />
</MyClass>

Complex struct is marked as serializable, and being a structure, it has an implicit constructor without parameters. So why does not every complex object serialize its real and imaginary parts? Does this have to do with the fact that the 'Real' and Imaginary 'structure properties have getters but not setters?

Thank.

+3
source share
2 answers

XmlSerializer , ( public public, , ). :

  • System.Numerics.Complex , ( "full" )
  • MyClass ( ) IXmlSerializable.

.

public class StackOverflow_10523009
{
    public class MyClass : IXmlSerializable
    {
        public Complex[] ComplexNumbers;

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement("MyClass");
            List<Complex> numbers = new List<Complex>();
            while (reader.IsStartElement("Complex"))
            {
                Complex c = new Complex(
                    double.Parse(reader.GetAttribute("Real")),
                    double.Parse(reader.GetAttribute("Imaginary")));
                numbers.Add(c);
                reader.Skip();
            }

            reader.ReadEndElement();
            this.ComplexNumbers = numbers.ToArray();
        }

        public void WriteXml(XmlWriter writer)
        {
            foreach (var complex in ComplexNumbers)
            {
                writer.WriteStartElement("Complex");
                writer.WriteStartAttribute("Real"); writer.WriteValue(complex.Real); writer.WriteEndAttribute();
                writer.WriteStartAttribute("Imaginary"); writer.WriteValue(complex.Imaginary); writer.WriteEndAttribute();
                writer.WriteEndElement();
            }
        }

        public override string ToString()
        {
            return "MyClass[" + string.Join(",", ComplexNumbers) + "]";
        }
    }
    public static void Test()
    {
        MyClass mc = new MyClass { ComplexNumbers = new Complex[] { new Complex(3, 4), new Complex(0, 1), new Complex(1, 0) } };
        XmlSerializer xs = new XmlSerializer(typeof(MyClass));
        MemoryStream ms = new MemoryStream();
        xs.Serialize(ms, mc);
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
        ms.Position = 0;
        MyClass mc2 = (MyClass)xs.Deserialize(ms);
        Console.WriteLine(mc2);
    }
}
+2

, .
, , :

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();
    }
}

, System.Xml.Serialization, - , :

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());
    }
}

, , XmlSerializer ( m_real m_imaginary Complex).

+3

All Articles