3

Complex[]这是我在序列化对象时得到的 XML 输出:

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

Complex结构被标记为可序列化的,并且作为结构,它具有隐式的无参数构造函数。那么为什么不是每个 Complex 对象都序列化它的实部和虚部呢?这是否与结构的'Real'和 ' Imaginary ' 属性具有 getter 而不是 setter 的事实有关?

谢谢。

4

2 回答 2

3

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).

于 2012-05-09T20:19:40.513 回答
2

XmlSerializer不序列化没有 setter 的属性(IIRC 它只序列化具有公共 getter 和 setter 的公共属性)。你有几个选择:

  • System.Numerics.Complex类型替换为您创建的类型(并具有“完整”属性)
  • 通过接口更改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);
    }
}
于 2012-05-09T20:59:58.467 回答