Data Binding with hieracherial XML file


  • Hello,

    I am having a lot of trouble doing what appears to be a basic task, hopefully someone here can help.

    I have an XML file that contains nested information for a quiz.  Here is a sample of the file:



      <Lesson Name="Lesson 1, All About Monkeys>

       <Question Text ="This is question 1"/>

       <Question Text ="This is question 2"/>


      <Lesson Name="Lesson 2, All About Chickens>

       <Question Text ="This is question 1"/>

       <Question Text ="This is question 2"/>




    <Lesson Name = "Sports Cars>





    Now what I would like to do is bind it to my control, and be able to jump from 'table' to ‘table' , i.e. Cars, then get the first lesson, then those questions, then the next etc

    I know I can do this parsing it all myself, but I would like to be able to take advantage of the data binding, and when I view the xml file in datagrid view in VS, I can drill down the relations to the right info, but I cant tell how to produce code that will do it.


    I’m using c# VSTS 2005

    Thank you,

    Wednesday, December 28, 2005 12:09 AM

All replies

  • Load the above in a DataSet, it should give you a central table called Lesson with the various questions in it. Also it should give you various other tables such as Animals, Cars etc.

    The Lesson Table should have FK/DataRelation with Animal table using different columns, which should allow you to zero in on the proper lesson ID. Then you can jump to the Question Table and using the appropriate LessonID, you should be able to zero in on the appropriate questions.

    How do you do this in code?


    Then add appropriate datarelations

    Then populate a dropdown with DataSet.Tables, you may need to add a binding to bind with TableNames. On the OnChange event of the dropdown, find appropriate datarows as follows -

    SelectedTable = ds.Tables[selectedtablename]
    SelectedTable.IdColumn.GetParentRow .. . GetChildRows (of course specify appropriate data relations).

    That should return you a DataRow[] - databind that to the appropriate datagrid view.

    Easy :)

    Sahil Malik [MVP C#]
    Author: Pro ADO.NET 2.0

    Wednesday, December 28, 2005 4:57 AM
  • Thank you for the information, can you elaborate in one place though. can you give a example of adding the appropriate data relations

    I have been able to hack my way around and drill down one level, but trying to get the questions for the LessonID  I'm not getting that relation.

    Thanks again for your help!



    Wednesday, December 28, 2005 8:04 PM