views:

317

answers:

1

Hello all,

for some time now I have been trying to solve the following problem and I'm starting to run out of ideas:

I have generated a set of C# classes from an xsd schema using the xsd.exe tool and deserializing xml files works fine. The problem is that apart from the convenience and safety of using the auto generated classes, I also need information about the xml hierarchy, ie I need to establish parent-child relationships between the objects created during deserialization. Note that I want to avoid keeping a separate xml hierarchy structure (like a DOM tree), but rather make the generated objects keep track of their parents and children.

I have managed to pull this off in java using JAXB by:

  1. Defining a common base class for all deserialized objects. This base class contains a list of children and a reference to a parent object (if any).

  2. Using the Unmarshaller.Listener functionality that provides a callback on completed object deserialization. This callback provides a reference to the parent of the recently deserialized object, which makes establishing parent-child relationships trivial.

How would I go about doing this in C#? I have had a look at the MSDN docs and done quite a lot of googling, but haven't been able to find any useful information.

Any help would be much appreciated.

/K

A: 

XmlSerializer should maintain simple object hierarchies for serialization and deserialization. Complex things such as arrays or lists containing more than one type of object are a bit tricker. . . but possible.

Jason D