2011-05-03 149 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 
    } 
}