Getting atrributes from child's descenant

66 views Asked by At

So I have this code that's supposed to make my xml file read out each descendant element's attribute after selecting the previous element. Here's the xml I'm using -

<?xml version="1.0" encoding="utf-8" ?> 
<adventures>
  <adventure_path Name ="Adventure Path 1">
    <adventure Name ="Adventure 1">
      <senario Name ="Senario 1">
        <location Name="Location 1" Players="1"/>
        <location Name="Location 2" Players="1"/>
      </scenario>
      <senario Name ="Senario 2">
        <location Name="Location 3" Players="1"/>
        <location Name="Location 4" Players="1"/>
      </scenario>
    </adventure>
    <adventure Name="Addventure 2">
      <senario Name ="Senario 3">
        <location Name="Location 5" Players="1"/>
        <location Name="Location 6" Players="1"/>
      </scenario>
    </adventure>
  </adventure_path>
  <adventure_path Name ="Adventure Path 2">
    <adventure Name ="Adventure 3">
      <senario Name ="Senario 4">
        <location Name="Location 7" Players="1"/>
        <location Name="Location 8" Players="1"/>
      </scenario>
      <senario Name ="Senario 5">
        <location Name="Location 9" Players="1"/>
        <location Name="Location 10" Players="1"/>
      </scenario>
    </adventure>
  </adventure_path>
</adventures>

So basically what should happen the program lists all the adventure paths in listbox1, I select one of said adventure paths. The program lists all adventures within the selected adventure path, I select one. Finally the program lists all scenarios in the selected adventure. What currently happens though is it'll do the first two lists perfectly, but when I select the adventure in the second list, I can't seem to get it to list any of the scenarios. It doesn't crash, just doesn't list them. Any help would be great and here's my code for what should make all the scenarios list.

 private void lst_Adventures_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string selectedItem = lst_Adventure.SelectedItem.ToString();
    string selectedAdventure = lst_Adventures.SelectedItem.ToString();

    lst_Senarios.Items.Clear();

    System.Console.WriteLine(selectedItem);

    XDocument doc = new XDocument();

    doc = XDocument.Load("D:\\WpfApplication1\\WpfApplication1\\Adventures.xml");

    XElement selectedElement = doc.Descendants().Where(x => (string)x.Attribute("Name") == selectedItem).FirstOrDefault();
    XElement selectedAdventures = selectedElement.Descendants().Where(x => (string)x.Attribute("Name") == selectedItem).FirstOrDefault();

    if (selectedAdventures != null)
    {
        foreach (var docs in selectedAdventures.Elements("senario"))
        {
            string AdventuresPathName = docs.Attribute("Name").Value;
            lst_Adventures.Items.Add(AdventuresPathName);
        }
    }
}
1

There are 1 answers

0
har07 On

I've just tried your code with the xml you provide, and i noted some flaws. When those get corrected your code will run as expected (if i dont misunderstand what you expect):

  1. Opening tag for senario doesnt match the closing tag scenario. Dont forget to change "senario" to "scenario" in foreach if you changed the opening tag to scenario.
  2. selectedAdventures should be matched against selectedAdventure instead of selectedItem (if i dont misunderstand the variables)