把XML中的Code读出来,然后加入ComboBox的Items就可以了。
读取XML文件,比较简单的方法蛇XmlTextReader.读取XML的代码:
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(filePath);
reader.WhitespaceHandling = WhitespaceHandling.None; while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
{
// 这里reader.Name 就是Node的名称
}
break;
default:
break;
}
}
}
读取XML文件,比较简单的方法蛇XmlTextReader.读取XML的代码:
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(filePath);
reader.WhitespaceHandling = WhitespaceHandling.None; while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
{
// 这里reader.Name 就是Node的名称
}
break;
default:
break;
}
}
}
我使用DATASET方法
DataSet xmlread = new DataSet();
xmlread.ReadXml("XMLFile1.xml");
comboBox1.DataSource = xmlread.Tables[0];
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "age";
<persons>
<person>
<name>zhangsan</name>
<age>10</age>
</person>
</persons>
又遇到点新问题,我利用combox来是实现有选择性加密XML文档节点.代码如下
DataSet ds = new DataSet("test");
ds.ReadXml(filePath);
DataTable dt = ds.Tables["person"];
foreach (DataRow rowNameList in dt.Rows)
{
comboBox1.Items.Add(rowNameList["name"].ToString());
comboBox1.Items.Add(rowNameList["age"].ToString());
}
在C#winform下,XML语法里怎么调用combox所选择的值
string friendName = comboBox1.Text.Trim();?????XML选择节点的加密方法,我只知道这两种:
XmlElement xmlElemt = xmlDoc.SelectSingleNode("name") as XmlElement;
XmlElement xmlElemt = xmlDoc.GetElementsByTagName("name")[0] as XmlElement;
用我上面給你貼出來的代碼就可以選擇comboBox1.SelectedValue即我上面那樣的對應的age的值。
然后再把所选择的节点值传递到加密类里.
xmlread.ReadXml("XMLFile1.xml");
comboBox1.DataSource = xmlread.Tables[0];
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "age";這樣在comboBox1中顯示的是﹕zhangsan
lisi
當你選擇﹕zhangsan
用comboBox1.SelectedValue取得的值就是:10
即﹕string str=comboBox1.SelectedValue.ToString().Trim();這里的str就是:10