遍历XML的一个示例----dhgList

http://www.cnblogs.com/xiaobaidhg/archive/2006/07/06/443965.html

1、xml文件MyConfigure.xml

<?xml version="1.0" encoding="utf-8" ?> 
<IPConfigure>
    <IPID id="">
      <IPAddress></IPAddress>
      <IPUser></IPUser>
      <IPPass></IPPass>
      <IPLocalPath></IPLocalPath>
      <IPThread></IPThread>
    </IPID>
</IPConfigure>

2、遍历该xml文件。
/// <summary>
  /// 读入xml的值--dhg 2006-7-6 10:00
  /// </summary>
  private void MyXmlReader()
  {
   try
   {
    string XMLPath=Application.StartupPath+"MyConfigure.xml";
    XmlDocument doc=new XmlDocument();
    doc.Load(XMLPath);
    XmlNode xnuser=doc.SelectSingleNode("IPID").ChildNodes;//找到所有的IPConfigure下的所有子节点
    foreach(XmlNode xn in xnuser)  //遍历IPID下所有的节点
    { 
     XmlElement xe=(XmlElement)xn;
     //读取节点中的一个属性
     XmlNodeList Ipid=xe.SelectNodes(" /IPID/@id ");
      //XmlNode xnuser=doc.SelectSingleNode("IPID");
      //string flag=xnuser.Attributes["id"].InnerText;    
     XmlNodeList ipaddress=xe.GetElementsByTagName("IPAddress");
     XmlNodeList ipuser=xe.GetElementsByTagName("IPUser");
     XmlNodeList ippass=xe.GetElementsByTagName("IPPass");
     XmlNodeList iplocalpath=xe.GetElementsByTagName("IPLocalPath");
     XmlNodeList ipthread=xe.GetElementsByTagName("IPThread");
     if (Ipid.Count>0)
     {
      for(int i=0;i<Ipid.Count;i++)
      {
       if(Ipid[i].Value=Ipid.Count) //最后一个节点
       {
        this.txtIP.Text=ipaddress[i].InnerText.ToString();
        this.txtUser.Text=ipuser[i].InnerText.ToString();
        this.txtPass.Text=ippass[i].InnerText.ToString();
        this.txtlocalPath.Text=iplocalpath[i].InnerText.ToString();
        this.txtChunksCount.Text=ipthread[i].InnerText.ToString();
       }
      }
     }
    }
   }
   catch(Exception e)
   {
        throw new Exception("Exception:{0}: ",e.ToString());
   }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值