[Solved] Serializing not-nested objects as nested


I’m going to take a crack at it here and assume that with the info you’ve given you need a class structure like this:

class Book
{
    [XmlAttribute("bookAttribute")]
    public string bookAttribute = "";

    [XmlElement("Shelf")]
    List<Shelf> Shelves = new List<Shelf>();
}

class Shelf
{

}

Then when you create the programmatic relationship between a book and shelves (which seems backward to me – but I don’t know what the whole scenario is) you can then loop it during your “Save” routine to write it as XML.

In the event that the original structures cannot be modified, you can still derive classes from them:

class ExpandedBook: Book
{
    [XmlAttribute("bookAttribute")]
    public string bookAttribute = "";

    [XmlElement("Shelf")]
    List<Shelf> Shelves = new List<Shelf>();
}

…unless the class is sealed in which case you’ll have to create ugly arrays of arrays.

0

solved Serializing not-nested objects as nested