Click here to Skip to main content
12,767,637 members (52,156 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML LINQ Silverlight
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:

<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:

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:

 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 29-Jul-12 15:22pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
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.
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 29 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100