先上XML代码(此文件放在bin下面):
<?xml version="1.0" encoding="utf-8" ?>
<Face>
<MCU>
<MCU1>
<PhysicalID>9700</PhysicalID>
<PANID>2300</PANID>
<MapID>2400</MapID>
<RI>
<CCTAddress>0000</CCTAddress>
<Address>6523</Address>
<Address>6524</Address>
</RI>
</MCU1>
<MCU2>
<PhysicalID>9701</PhysicalID>
<PANID >2301</PANID>
<MapID>2401</MapID>
<RI>
<CCTAddress>0000</CCTAddress>
<Address>6525</Address>
<Address>6526</Address>
<Address>6527</Address>
</RI>
</MCU2>
<MCU3>
<PhysicalID>9702</PhysicalID>
<PANID>2302</PANID>
<MapID>2402</MapID>
<RI>
<CCTAddress>0000</CCTAddress>
<Address>6528</Address>
<Address>6529</Address>
<Address>6530</Address>
<Address>6531</Address>
</RI>
</MCU3>
</MCU>
<RPT>
<RPT1>
<PhysicalID>9703</PhysicalID>
<PANID>2303</PANID>
<MapID>2403</MapID>
<RI>
<CCTAddress>0000</CCTAddress>
<Address>6532</Address>
<Address>6533</Address>
</RI>
</RPT1>
<RPT2>
<PhysicalID>9704</PhysicalID>
<PANID>2304</PANID>
<MapID>2404</MapID>
<RI>
<CCTAddress>0000</CCTAddress>
<Address>6534</Address>
<Address>6535</Address>
<Address>6536</Address>
<Address>6537</Address>
<Address>6538</Address>
<Address>6539</Address>
</RI>
</RPT2>
</RPT>
<CCTRF>
<PhysicalID>9705</PhysicalID>
<PANID>2305</PANID>
<MapID>2405</MapID>
<RI>
<CCTAddress>0000</CCTAddress>
<Address>6540</Address>
<Address>6541</Address>
</RI>
</CCTRF>
</Face>
在一个Form上有两个ComboBox(cmbMCU,cmbRPT),7个TextBox(txtRPT1,txtRPT2.........txtRPT7)
Load里面
cmbMCU.Items.AddRange(new string[] { "MCU1", "MCU2", "MCU3" });
cmbRPT.Items.AddRange(new string[]{"RPT1","RPT2"});
由于获取节点的个数不一定,将节点的值设置到Textbox里面则先要获取到Textbox,可以用下面的方法根据一个索引来获取一个TextBox
方法如下:
/// <summary>
/// 由于要设置多个Textbox,如果个数是固定的还可以用多条语句设置
/// 但个数是不一定的,所要一个根据索引来获取那个TextBox
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
private TextBox GetTextBox(int i)
{
switch (i)
{
case 1: return txtRPT1;
case 2: return txtRPT2;
case 3: return txtRPT3;
case 4: return txtRPT4;
case 5: return txtRPT5;
case 6: return txtRPT6;
case 7: return txtRPT7;
}
return null;
}
查询XML节点,将查询的值赋给TextBox
private void SetText(string selectText)
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load("DeviceFace.xml");
XmlNode root = xmlDoc.SelectSingleNode(string.Format("//{0}", selectText));
if (root != null)
{
txtPhyID.Text = (root.SelectSingleNode("PhysicalID")).InnerText;
txtPANID.Text = (root.SelectSingleNode("PANID ")).InnerText;
txtPANID2.Text = (root.SelectSingleNode("PANID ")).InnerText;
txtMAPID.Text = (root.SelectSingleNode("MapID")).InnerText;
txtCCTADD.Text = "0000";
root = xmlDoc.SelectSingleNode(string.Format("//{0}//RI", selectText));
if (root != null)
{
int index = 1;
XmlNodeList xnList = xmlDoc.SelectNodes(string.Format("//{0}//RI//Address", selectText));
foreach (XmlNode xn in xnList)
{
string AddressValue = xn.InnerText;
GetTextBox(index).Text = AddressValue;
index++;
}
while (index < 8)
{
GetTextBox(index).Text = "65535";
index++;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
两个ConboBox的事件如下:
private void cmbMCU_SelectedIndexChanged(object sender, EventArgs e)
{
SetText(cmbMCU.Text);
}
private void cmbRPT_SelectedIndexChanged(object sender, EventArgs e)
{
SetText(cmbRPT.Text);
}