Getting XML data into combobox

前端 未结 2 1191
情深已故
情深已故 2021-01-24 08:12

I have a windows form with 3 comboboxes and a XML file as following



    
        

        
2条回答
  •  旧巷少年郎
    2021-01-24 08:22

    Usinq LINQ To XML:

    var xml = XElement.Parse(
                    @"
                            
                                
                                    
                                        Ctrl
                                    
                                    
                                        Alt
                                    
                                    
                                        Shift
                                    
                                
                            
                            
                                
                                    
                                        Ctrl
                                    
                                    
                                        Alt
                                    
                                    
                                        Shift
                                    
                                
                            
                            
                                
                                    
                                        a
                                    
                                    
                                        b
                                    
                                    
                                        c
                                    
                                
                            
                        ");
    
    var key1 = xml.Descendants("key1");
    
    foreach (var key in key1)
        comboBox1.Items.Add(key.Value.Trim());
    
    var key2 = xml.Descendants("key2");
    
    foreach (var key in key2)
        comboBox2.Items.Add(key.Value.Trim());
    
    //Do the same for other keys...
    

提交回复
热议问题