-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. 我需要填写系列、型号和年份的组合框。

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

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

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

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

4

1 回答 1

1

我希望我做对了...我创建了一个小样本表格。在组合框的SelectedIndexChanged上,我将显示过滤后的aucCars。ComboBoxes根据给定的详细信息从 xml 文件中获取数据(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()根据组合框中的选择进行过滤并在列表框中显示结果。如果您想在列表框中显示特定元素,请添加新的 linq stmt 并选择相应的元素

于 2012-09-23T08:39:45.880 回答