2012-09-23 27 views
-1
  1. 我正在使用C#来执行此操作。
  2. 好吧,这就是我的XML文件的样子......只有更多的AUCCars节点。根据用户选择填充列表框

    <Recordset> 
    <AUCCars> 
        <Web_Series>5 Series</Web_Series> 
         <Body_Desc>Sedan</Body_Desc> 
         <Model_Type>550i (E60)</Model_Type> 
         <Model_Year>2006</Model_Year> 
         <Ext_Colour>Alpine White III - Non-Metallic</Ext_Colour> 
         <Int_Colour>Leather Dakota Black - Dakota Leather</Int_Colour> 
         <Price>R 579000</Price> 
         <Search_Price>579000</Search_Price> 
         <Province>Gauteng</Province> 
         <AucID>106288</AucID> 
         <DealerID>45</DealerID> 
         <DealerCode>29968</DealerCode> 
         <DealerName>Lyndhurst Auto</DealerName> 
         <Transmission>Automatic</Transmission> 
         <AirCon>n/a</AirCon> 
         <Radio>Yes</Radio> 
         <PSteering>Yes</PSteering> 
         <ABS>Yes</ABS> 
         <Airbag>Yes</Airbag> 
         <Sunroof>Yes</Sunroof> 
         <Km>11500</Km> 
         <Motorplan>Yes</Motorplan> 
         <Warranty>N</Warranty> 
         <BodyNo>6CR76051</BodyNo> 
         <ModelOEM>NB52</ModelOEM> 
         <ColourOEM>300</ColourOEM> 
         <TrimOEM>LCSW</TrimOEM> 
         <WheelsOEM></WheelsOEM> 
         <Sold>N</Sold> 
         <Notes> </Notes> 
         <Moreoptions>Automatic Transmission with Steptronic 
            Interior trim finishers, Fine-wood, Poplar Grain Brown, high-gloss 
            Park Distance Control (PDC),front and rear</Moreoptions> 
         <Picture>\Vehicle_Pictures\E60\LI\frontview_big_P0300.jpg</Picture> 
         <PictureRear>\Vehicle_Pictures\E60\LI\rearview_big_P0300.jpg</PictureRear> 
         <Interior>\Vehicle_Pictures\E60\LI\Interior\big_Fo_LCSW.jpg</Interior> 
    </AUCCars> 
    <AUCCars> 
         <Web_Series>5 Series</Web_Series> 
         <Body_Desc>Sedan</Body_Desc> 
         <Model_Type>550i (E60)</Model_Type> 
         <Model_Year>2006</Model_Year> 
         <Ext_Colour>Black Sapphire - Metallic</Ext_Colour> 
         <Int_Colour>Amethyst Black Exclusive Leather - Exclusive Leather</Int_Colour> 
         <Price>R 529990</Price> 
         <Search_Price>529990</Search_Price> 
         <Province>KwaZulu Natal</Province> 
         <AucID>111922</AucID> 
         <DealerID>17</DealerID> 
         <DealerCode>2485</DealerCode> 
         <DealerName>Supertech</DealerName> 
         <Transmission>Automatic</Transmission> 
         <AirCon> Yes</AirCon> 
         <Radio>Yes</Radio> 
         <PSteering>Yes</PSteering> 
         <ABS>Yes</ABS> 
         <Airbag>Yes</Airbag> 
         <Sunroof>n/a</Sunroof> 
         <Km>7000</Km> 
         <Motorplan>n/a</Motorplan> 
         <Warranty>N</Warranty> 
         <BodyNo>6CR75567</BodyNo> 
         <ModelOEM>NB52</ModelOEM> 
         <ColourOEM>475</ColourOEM> 
         <TrimOEM>LDRH</TrimOEM> 
         <WheelsOEM></WheelsOEM> 
         <Sold>N</Sold> 
         <Notes> </Notes> 
         <Moreoptions>Automatic Transmission with Steptronic 
            Electric Rear Screen Roller Sun Blind with manual Side Blinds 
            Interior trim finishers, Fine-wood, Poplar Grain Brown, high-gloss 
            High Beam Assist 
            Head-up display (not with SA354)</Moreoptions> 
         <Picture>\Vehicle_Pictures\E60\LI\frontview_big_P0475.jpg</Picture> 
         <PictureRear>\Vehicle_Pictures\E60\LI\rearview_big_P0475.jpg</PictureRear> 
         <Interior>\Vehicle_Pictures\E60\LI\Interior\big_Fo_LDRH.jpg</Interior> 
    </AUCCars> 
    <Recordset> 
    
  3. 我需要填充Series,Model和Year的组合框。

  4. 这我说得对。当我编译程序时,它会将所有系列加载到组合框
  5. 组合框中。
  6. 当我选择一个系列时,它会自动将所有模型加载到模型组合
  7. 框中。当我选择一个模型时,它会相应地自动加载年份。

  8. 现在我需要根据选择显示数据。

  9. 例如,我为系列组合框选择了“5系列”,模型组合框11选择了“550i(E60)”,年份组合框选择了“2006”。

  10. 如何根据我的选择显示该特定节点中的所有数据13.在列表框中?

回答

1

我希望我没有让你正确的...我创建了一个小样本形式。 On SelectedIndexChanged of a comboBox我会显示已过滤aucCars。组合框根据根据选择的组合框并显示结果列表框的问题(Web_Series, Model_Type, Model_Year - distinct)

// get data from file 
XElement aucCars = XElement.Load("data.xml"); 
private void cmbSeries_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (cmbSeries.SelectedItem != null) 
    { 
     string currentSeries = cmbSeries.SelectedItem.ToString(); 
     var models = (from a in aucCars.Elements() 
         where a.Element("Web_Series").Value == currentSeries 
         select a.Element("Model_Type").Value).Distinct().ToList(); 
     cmbModel.DataSource = models; 
    } 
    showAucCars(); 
} 
private void cmbModel_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (cmbModel.SelectedItem != null) 
    { 
     string currentSeries = cmbSeries.SelectedItem.ToString(); 
     string currentModel = cmbModel.SelectedItem.ToString(); 

     var years = (from a in aucCars.Elements() 
         where a.Element("Web_Series").Value == currentSeries && 
         a.Element("Model_Type").Value == currentModel 
         select a.Element("Model_Year").Value).Distinct().ToList(); 
     cmbYear.DataSource = years; 
    } 
    showAucCars(); 
} 
private void cmbYear_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    showAucCars(); 
} 
private void frmXmlLoad_Load(object sender, EventArgs e) 
{    
    var series = (from a in aucCars.Elements() 
       select a.Element("Web_Series").Value).Distinct().ToList(); 
    cmbSeries.DataSource = series; 
} 
private void showAucCars() 
{ 
    var filterCars = aucCars.Elements(); 
    if (cmbSeries.SelectedItem!=null) 
    { 
     string currentSeries = cmbSeries.SelectedItem.ToString(); 
     filterCars = from a in filterCars 
         where a.Element("Web_Series").Value == currentSeries 
         select a; 
    } 
    if (cmbSeries.SelectedItem != null) 
    { 
     string currentModel = cmbModel.SelectedItem.ToString(); 
     filterCars = from a in filterCars 
         where a.Element("Model_Type").Value == currentModel 
         select a; 
    } 
    if (cmbSeries.SelectedItem != null) 
    { 
     string currentYear = cmbYear.SelectedItem.ToString(); 
     filterCars = from a in filterCars 
         where a.Element("Model_Year").Value == currentYear 
         select a; 
    } 
    // will show all the element data 
    // add a new linq stmt to select specific elements 
    listBox1.DataSource = filterCars.ToList();    
} 

提供的细节在showAucCars()过滤器从XML文件得到他们数据。如果您想要在列表框中显示特定元素,请添加一个新的linq stmt并选择相应元素

+0

谢谢你的时间!这正是我想要的! –

+0

很高兴我可以帮助 - 在回报你可能会考虑**投票我的答案**和/或**标记**我的回应作为最佳答案/ solution_! –