从网络上学到几种读取XML文件的方法 后,我通过实践,发现也能不用遍历的方式直接读取到XML节点的数据。
方法如下:
<?xml version="1.0" encoding="GB2312"?> <Main> <StartCheckBox>False</StartCheckBox> <StaShutCheckBox>False</StaShutCheckBox> <ShutDTPickerCH>True</ShutDTPickerCH> <ShutDTPicker>14:37:34</ShutDTPicker> <RestRDTPicker>00:40:40</RestRDTPicker> <SelInTimecomboBox>20</SelInTimecomboBox> </Main>
C#代码内容:
XmlDocument doc = new XmlDocument(); doc.Load("MCTMConfig.xml"); XmlNodeList nodelist = doc.SelectSingleNode("Main").ChildNodes; StartCheckBox.Checked = Convert.ToBoolean(nodelist[0].InnerText); StaShutCheckBox.Checked = Convert.ToBoolean(nodelist[1].InnerText); ShutDTPicker.Checked = Convert.ToBoolean(nodelist[2].InnerText); if (ShutDTPicker.Checked) { ShutDTPicker.Value = Convert.ToDateTime(nodelist[3].InnerText); } RestRDTPicker.Value = Convert.ToDateTime(nodelist[4].InnerText); ShutCBox.Text = Convert.ToString(nodelist[5].InnerText); doc.RemoveAll();直接读取更灵活,而且代码更容易插入到其他地方。