XMLSerializer.Deserialize(), Order not applying

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im attempting to serialize/deserialize some XML, and expected that [XmlElement(Order = n)] would applying during deserialization... does this simply NOT happen? What IS the expected order of deserialization?

Given the following, the output is
Set Nodes [this is the first setting of Nodes] <br/>
Set Nodes [this is the 1st property deserialized, but its Order = 2 and its not even first or last alphabetically]<br/>
Set Rows [its Order = 0 but its 2nd]<br/>
Set Columns [its Order = 1, but its 3rd]

<pre class="prettyprint" style=" public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DummyClass dummyClass = new DummyClass();
dummyClass.Nodes = new List<DummyNode>();

using (TextWriter writer = new StreamWriter(@"c:test.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(DummyClass));
serializer.Serialize(writer, dummyClass);
}

using (TextReader reader = new StreamReader(@"c:test.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(DummyClass));
serializer.Deserialize(reader);
}
}
}

[Serializable]
public class DummyClass
{
private int _rows;

[XmlElement(Order = 0)]
public int Rows
{
get { return _rows; }
set { _rows = value;

Console.WriteLine("Set Rows");
}
}

private int _columns;

[XmlElement(Order = 1)]
public int Columns
{
get { return _columns; }
set { _columns = value;

Console.WriteLine("Set Columns");
}
}

private List<DummyNode> _nodes;

[XmlElement(Order = 2)]
public List<DummyNode> Nodes
{
get { return _nodes; }
set { _nodes = value;

Console.WriteLine("Set Nodes");
}
}
}

[Serializable]
public class DummyNode
{
public char Value { get; set; }
}[/code]

The scenario is that I have DummyNode[,] property, but you cant serialize multidimensional arrays. So I figured, Ill expose an List<DummyNode> (should be [], not List<> I guess), and in its setter, use the Rows/Cols value to set the multi-dim
array... but Nodes is being deserialized FIRST so Rows/Cols arent set. Whats another way to handle this?

Warm regards,
Matt
<br/>

View the full article
 
Back
Top