Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>XmlSerializer</code> doesn't serialize properties which don't have a setter (IIRC it only serializers public properties with both public getter and setters). You have a few options:</p> <ul> <li>Replace the <code>System.Numerics.Complex</code> type with a type which you create (and has a "full" property)</li> <li>Change the <code>MyClass</code> class to handle the serialization (and deserialization) of the complex numbers, via the <code>IXmlSerializable</code> interface.</li> </ul> <p>The second option is shown below.</p> <pre><code>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&lt;Complex&gt; numbers = new List&lt;Complex&gt;(); 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload