0

So I have a list of different Xml elements under a parent sort of like this:

    <Group>
      <Circle Radius="5"/>
      <Rectangle Height="10" Width="15"/>
      <Rectangle Height="7" Width="8"/>
      <Circle Radius="12"/>
    </Group>

And the classes:

    public class Group
    {
      [XmlElement("Circle")]
      public List<Circle> Circles { get; set;}

      [XmlElement("Rectangle")]
      public List<Circle> Circles { get; set;}
    }

    public class Circle
    {
      // The position it was encountered in the list of elements
      public int Position { get; set; }

      [XmlAttribute("Radius")]
      public string Radius { get; set;}
    }

    public class Rectangle
    {
      // The position it was encountered in the list of elements
      public int Position { get; set; }

      [XmlAttribute("Width")]
      public string Width { get; set; }

      [XmlAttribute("Length")]
      public string Length { get; set; }
    }

So essentially what I'd like to do here is to save the order in which these different elements occurred as child elements under their parent, saving their place in the position integer. The only solution I can think of is to have a static variable that counts instantiations of these objects and set the position by using the count of said variable. I'd like to believe there is a 'cleaner' way of handling this and would appreciate any suggestions.

Edit: To be more clear as to where I'm going with this I want to take the two lists (because they are different objects with different attributes) and convert them to another class with a shared base class. So I have a list of these base class elements that are ordered the same as they were declared in xml so that I can draw them to the screen in that order (think a stackpanel). So if inheritance is possible during Xml deserialization that may also fix my problem if they can automatically be parsed into a list of the base class.

Thermonuclear
  • 352
  • 3
  • 18

2 Answers2

1

I think to help you

[XmlElementAttribute(Order = 1)]

Visit Controlling order of serialization in C#

Community
  • 1
  • 1
Arwen
  • 21
  • 2
  • No, that dictates how properties inside a class are serialized. OP wants to dictate the order in which a list's elements are serialized. Furthermore, if you think a question is answered in another question (and only if you're sure), flag the former question to close it as a duplicate of the latter. – CodeCaster Apr 09 '15 at 15:27
  • I appreciate the quick response, and is good information, however this does not solve my problem. I need to store the order in which they occur in the xml so when I draw say my circles and rectangles to the screen they will occur in the same order as they were declared under the group. – Thermonuclear Apr 09 '15 at 15:31
1

You are defining the group element wrong. You should probably define it as a single list and keep the order in the list correctly.

The list should be of type object (or a common base type).

see https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute%28v=vs.110%29.aspx

Yishai Galatzer
  • 8,791
  • 2
  • 32
  • 41
  • As soon as I added that edit I started looking into inheritance during deserialization and found essentially that. Didn't even think that it may be possible until now. Thank you sir, this is exactly what I wanted. – Thermonuclear Apr 09 '15 at 15:49