Modeling an XML hierarchy for traversal with MVVM

40 views Asked by At

I have a good sized (4 MB) XML file that looks like this:

<A>
  <B>
    <C>
      <D>
      <D>
      ...

There are many C's and many B's. There is one A. What I am trying to do is represent each level with a class that includes a GetChildren method which queries the XML file and returns a List<> of child objects. So calling ClassA.GetChildren() would return List<ClassB> and so on.

The second capability I want is for the user to be able to walk the hierarchy. So in the UI, I will display a list of the B objects. When the user clicks on a B, I want to drill in and re-populate the list with all of the C's under the selected B.

I am doing the MVVM pattern here, so I have a ViewModel where I would like to keep track of the current object. I guess I will also need to keep track of the parent objects too, so the user can use breadcrumbs to walk back up.

So I am looking for assistance in implementing this data model. I have tried a number of variations but can't quite cover all the bases. Currently I have this:

interface IMyData<T>
{
    List<T> GetChildren();
}

And for each node A, B, C, and D, I have a class like so:

public class ClassB : IMyData<ClassC>
{
    public List<ClassC> GetChildren()
    {
       ...
    }
}

One thing I am unable to do with this setup is store the current object in a single property, because of the way IMyData is defined with a generic. I feel like there must be an elegant way to do this but it's just not coming to me.

0

There are 0 answers