[原创]遍历XML的一个示例----dhg
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());
}
}