Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I'm stumped with this one.

on my silvelrght app, I have 2 comboboxes, that are supposed to populate from an xml data sheet. So lets say here's a data sample:

XML
<model name="Accord 4DR">
    <code name="CP2E6CE" />
    <code name="CP2F6CE" />
    <code name="CP2F7CJ" />
    <code name="CP2F8CJN" />
    <code name="CP2F8CKN" />
    <code name="CP3F8CJN" />
    <code name="CP3F8CKN" />
  </model>

  <model name="Civic 2DR">
    <code name="FG3A4CE" />
    <code name="FG3B4CE" />
    <code name="FG3A5CJ" />
    <code name="FG3B5CJ" />
    <code name="FG3B9CKN" />
    <code name="FG4A5CK" />
  </model>



For the first combobox, it populates likes this:

C#
protected override void OnNavigatedTo(NavigationEventArgs e)
       {

           XDocument xml = XDocument.Load("modelcodes.xml");
           foreach (XElement element in xml.Descendants("model"))
           {
             CarTypeCB.Items.Add(element.FirstAttribute.Value);
           }
       }


Now, the second combobox needs to populate with the descendants of whatever selection I make on SelectionChanged of the first combobox. I cant figure out what the right syntax is for basically using the SelectedItem.Value to tell it where to get the descendants from. I want to do something similar so, id say something like:

C#
private void CarTypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
       {
           ModelCodeCB.IsEnabled = true;
           XDocument xml = XDocument.Load("modelcodes.xml");

           var query = from selection in xml.Descendants("model")
                       where (string)selection == (ModelCodeCB.SelectedValue)
                       from codes in selection.Elements("code")
                       select codes;

           foreach (XElement codes in query.Descendants("code"))
           {
               ModelCodeCB.Items.Add(codes.FirstAttribute.Value);
           }
       }


but it doesnt seem to respond and it doesn't populate the combobox. What would the proper syntax be?
Posted

1 solution

C#
private void CarTypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ModelCodeCB.IsEnabled = true;
            XDocument xml = XDocument.Load("modelcodes.xml");
 
            var query = from selection in xml.Descendants("model")
                        where (string)selection == (ModelCodeCB.SelectedValue)
                        from codes in selection.Elements("code")
                        select codes;
             
            foreach (XElement codes in query.Descendants("code"))
            {
                ModelCodeCB.Items.Add(codes.FirstAttribute.Value);
            }
        }

Try to replace for this:
C#
private void CarTypeCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ModelCodeCB.IsEnabled = true;
            XDocument xml = XDocument.Load("modelcodes.xml");
 
            var query = from selection in xml.Descendants("model")
                        where (string)selection == (ModelCodeCB.SelectedValue.ToString())
                        from codes in selection.Elements("code")
                        select codes;
             
            foreach (XElement codes in query)
            {
                ModelCodeCB.Items.Add(codes.FirstAttribute.Value);
            }
        }

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900