获取XML指定节点的值并显示在TextBox中

 先上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);
        }


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值