求教:从XML文件读取控件的属性值
XML文件如下:<?xml version="1.0" encoding="utf-8" ?>
<SaveConfig>
<BoxButtonInitial>
<ComboBoxSelectIndex>0</ComboBoxSelectIndex> //comboBox的SelectedIndex值
<textBox3>(local)</textBox3> //textBox3的Text值
<textBox4>ca</textBox4> //textBox4的Text值
<textBox5>ca</textBox5> //textBox5的Text值
<RadioButton1Checked>true</RadioButton1Checked> //RadioButton1的checked属性值
<RadioButton2-9Enabled>false</RadioButton2-9Enabled> //RadioButton2-9的Enabled属性值
<TextBox6-21Enable>false</TextBox6-21Enable> .....
<Button6Enabled>false</Button6Enabled> ......
</BoxButtonInitial>
</SaveConfig>
我想在一个Form的load事件里读取这些控件的设置,找了好的资料都弄不出来,特来请求高手解答,有没有其他好方法。谢谢!
我用的方法之一是:
XmlDocument doc = new XmlDocument();
doc.Load("XMLConf.xml"); //XMLConf.xml是上面的XML文件的文件名
XmlNode node1 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/ComboBoxSelectIndex");
comboBox1.SelectedIndex = Convert.ToUInt16(node1.Name); //这里有错,怎么改呢?
XmlNode node2 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/textBox3");
textBox3.Text = node2.Name.ToString();
XmlNode node3 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/textBox4");
textBox4.Text = node3.Name.ToString();
XmlNode node4 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/textBox5");
textBox5.Text = node4.Name.ToString();
XmlNode node5 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/RadioButton1Checked");
radioButton1.Checked = Convert.ToBoolean(node5.Name);
XmlNode node6 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/RadioButton2-9Enabled");
radioButton2.Checked = Convert.ToBoolean(node6.Name);
radioButton3.Checked = Convert.ToBoolean(node6.Name);
radioButton4.Checked = Convert.ToBoolean(node6.Name);
radioButton5.Checked = Convert.ToBoolean(node6.Name);
radioButton6.Checked = Convert.ToBoolean(node6.Name);
radioButton7.Checked = Convert.ToBoolean(node6.Name);
radioButton8.Checked = Convert.ToBoolean(node6.Name);
radioButton9.Checked = Convert.ToBoolean(node6.Name);
XmlNode node7 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/TextBox6-21Enable");
textBox6.Enabled = Convert.ToBoolean(node7.Name);
textBox7.Enabled = Convert.ToBoolean(node7.Name);
textBox8.Enabled = Convert.ToBoolean(node7.Name);
textBox9.Enabled = Convert.ToBoolean(node7.Name);
textBox10.Enabled = Convert.ToBoolean(node7.Name);
textBox11.Enabled = Convert.ToBoolean(node7.Name);
textBox12.Enabled = Convert.ToBoolean(node7.Name);
textBox13.Enabled = Convert.ToBoolean(node7.Name);
textBox15.Enabled = Convert.ToBoolean(node7.Name);
textBox16.Enabled = Convert.ToBoolean(node7.Name);
textBox17.Enabled = Convert.ToBoolean(node7.Name);
textBox19.Enabled = Convert.ToBoolean(node7.Name);
textBox20.Enabled = Convert.ToBoolean(node7.Name);
textBox21.Enabled = Convert.ToBoolean(node7.Name);
XmlNode node8 = doc.SelectSingleNode("SaveConfig/BoxButtonInitial/Button6Enabled");
button6.Enabled = Convert.ToBoolean(node8.Name);
[[it] 本帖最后由 jqc304 于 2008-10-11 13:04 编辑 [/it]]