Specifying the xml deserialization order
-
Hi, I think I've read about it before butnow I'm needing it, I can't find anything about it. How can I specify the xml deserialization order? Let me give an example. Imagine I have 3 parts in my class "School": classes, professors and students. I need to deserialize the following order (classes->professors->students) no matter how they are written in the xml. Thanks in advance.
-
Hi, I think I've read about it before butnow I'm needing it, I can't find anything about it. How can I specify the xml deserialization order? Let me give an example. Imagine I have 3 parts in my class "School": classes, professors and students. I need to deserialize the following order (classes->professors->students) no matter how they are written in the xml. Thanks in advance.
Mehdi_S wrote:
How can I specify the xml deserialization order?
By specifying the Order using the
XmlElement
attribute, similar to this;public class OrderedClass { private int field1; private string field2; private string field3; \[XmlElement(Order = 3)\] public string Field3 { get { return field3; } set { field3 = value; } } \[XmlElement(Order = 1)\] public int Field1 { get { return field1; } set { field1 = value; } } \[XmlElement(Order = 2)\] public string Field2 { get { return field2; } set { field2 = value; } } public OrderedClass() { field1 = 1; field2 = "String1"; field3 = "String2"; } }
The example is taken from MSDN[^], where you can download a sample application[^]. Good luck :)
I are Troll :suss:
-
Mehdi_S wrote:
How can I specify the xml deserialization order?
By specifying the Order using the
XmlElement
attribute, similar to this;public class OrderedClass { private int field1; private string field2; private string field3; \[XmlElement(Order = 3)\] public string Field3 { get { return field3; } set { field3 = value; } } \[XmlElement(Order = 1)\] public int Field1 { get { return field1; } set { field1 = value; } } \[XmlElement(Order = 2)\] public string Field2 { get { return field2; } set { field2 = value; } } public OrderedClass() { field1 = 1; field2 = "String1"; field3 = "String2"; } }
The example is taken from MSDN[^], where you can download a sample application[^]. Good luck :)
I are Troll :suss:
Thanks a lot. that helps much. just anaother question related to the same topic: knowing that I can define the set of types that a set can contains, can I specify the order of types deserialization. For example, I have the classes student and professor that inherit from the class person. the class persons contains the following set
[xmlarrayitem("Student", typeof(Student)),xmlarrayitem("Professor", typeof(Professor))]
[xmlarray("PersonsSet")]
public Person[] PersonsSet
{
get;set;
}How can I specify that the professor elements has to be all deserealized before the student elements no matter how they are ordered in the xml document. Thanks in advance.
-
Thanks a lot. that helps much. just anaother question related to the same topic: knowing that I can define the set of types that a set can contains, can I specify the order of types deserialization. For example, I have the classes student and professor that inherit from the class person. the class persons contains the following set
[xmlarrayitem("Student", typeof(Student)),xmlarrayitem("Professor", typeof(Professor))]
[xmlarray("PersonsSet")]
public Person[] PersonsSet
{
get;set;
}How can I specify that the professor elements has to be all deserealized before the student elements no matter how they are ordered in the xml document. Thanks in advance.
Mehdi_S wrote:
How can I specify that the professor elements has to be all deserealized before the student elements no matter how they are ordered in the xml document.
Taking your example; there's a difference in getting an order in the properties and getting an order in the items within the collection. You can change the order of the properties by adding those tags to the
Person
class. The collection would be serialized using that information. ImplementingISerializable
[^] or evenIXmlSerializable
[^] would be more appropriate for complexer structures. That way you can control the order of the properties and the data. There's a really nice CodeProject article on IXmlSerializable here[^]. Good luck :)I are Troll :suss: