Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Specifying the xml deserialization order

Specifying the xml deserialization order

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionxmltutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mehdi_S
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • M Mehdi_S

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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:

      M 1 Reply Last reply
      0
      • L Lost User

        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:

        M Offline
        M Offline
        Mehdi_S
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • M Mehdi_S

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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. Implementing ISerializable[^] or even IXmlSerializable[^] 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:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups