Hi,

I am trying to serialize an operation to an xml file and have a specific xml format.

I have 4 classes "Recipe" and the other 3 classes implement class "Recipe", being classes "Breakfast", "Lunch" and "Dinner". Class "Recipe" implements members "name" while the other 3 classes have the member "description". Also I have a list "recipeList" that holds the class instances then I serialize the instances to an xml file.

What I am trying to do is to have the xml output as such

<Recipe>
  <Breakfast>
    <Name>A</Name>
    <Name>B</Name>
  </Breakfast>
  <Lunch>
    <Name>C</Name>
  </Lunch>
  <Dinner>
    <Name>D</Name>
  </Dinner>
</Recipe>

rather than what it outputs at this time which is

<Recipe>
  <Breakfast>
    <Name>A</Name>
    <Name>B</Name>
  </Breakfast>
  <Lunch>
    <Name>C</Name>
  </Lunch>
  <Breakfast>
    <Name>D</Name>
  </Breakfast>
  <Dinner>
    <Name>E</Name>
  </Dinner>
</Recipe>

In other words, the elements are serialized to the xml file in the order that they are added, rather I am seeking to have the elements displayed as the former.

Thanks.

Sort recipeList before you output the XML.

Thanks Momerath, that was a good suggestion however what I am requesting is to have the various instances grouped together sharing one parent node, such as all Breakfast elements within the Breakfast node instead of separate but sequential when sorted.

Do you understand what I am asking? Thanks again.

I understand, and I believe there is a way to get it to do it automatically, but I'll have to do some research first :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.