我应该如何使用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);
}
答案 0 :(得分: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());
}
答案 1 :(得分: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
}
}