2011-05-03 146 views
0

我應該如何使用XML屬性填充組合框。我的XML文件是:使用XML屬性填充組合框?

<dataSources> 
    <dataSource id="1" name="support" dbtype="Oracle" dataSource="foo" initialCatalog="" userId="bar" password="x" /> 
</dataSources> 
<services> 

我需要用XML屬性名填充2個組合框。我也有下面的代碼,但現在我沒有得到所需的輸出?

XmlDocument doc = new XmlDocument(); 
doc.Load("abc.xml"); 
XmlNodeList colorList = doc.SelectNodes("config/dataSources"); 
foreach (XmlNode dataSources in colorList) 
{ 
    comboBox1.Items.Add(dataSources.InnerXml); 
} 

foreach (XmlNode dataSources in colorList) 
{ 
    comboBox2.Items.Add(dataSources.InnerXml); 
} 

回答

1

你想要的屬性名的值:

XmlDocument doc = new XmlDocument(); 
doc.Load("abc.xml"); 
XmlNodeList colorList = doc.SelectNodes("config/dataSources/dataSource"); 
foreach (XmlNode dataSources in colorList) 
{ 
    comboBox1.Items.Add(dataSources.Attributes["name"].Value.ToString()); 
} 
+0

確定會嘗試這 – anasooya 2011-05-03 08:06:56

0

試試這個:

foreach (XmlNode dataSources in colorList) 
{ 
    foreach(XmlAttribute attribute in dataSources.Attributes) 
    { 
     comboBox1.Items.Add(attribute.Name); // add the attribute name to cb1 
     comboBox2.Items.Add(attribute.Value); // add the attribute value to cb2 
    } 
}