XML文档如下:
<?xml version="1.0" encoding="utf-8"?>
<ftpHelp>
<ftpServer1 NetName="ne1" ServerIP="www.whvideo.cn" ServerPath="/logfiles/W3SVC180" LocalPath="D:/MyWork/download1" UserName="antistone" Password="loginlogout" TimeInterval="15">
</ftpServer1>
<ftpServerne2 NetName="ne2" ServerIP="ftp.sjtu.edu.cn" ServerPath="/html/css" LocalPath="D:/MyWork/download2" UserName="" Password="" TimeInterval="15" />
<ftpServerne3 NetName="ne3" ServerIP="219.231.160.2" ServerPath="/111" LocalPath="D:/MyWork" UserName="lm" Password="113762" TimeInterval="15" />
</ftpHelp>
XML文档中生成节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("FtpHelp.xml");
XmlNode node = xmlDoc.SelectSingleNode("ftpHelp");
String cndName = "ftpServer" + this.txtNeName.Text;
XmlElement cnd = xmlDoc.CreateElement(cndName);
cnd.SetAttribute("NetName", txtNeName.Text);
cnd.SetAttribute("ServerIP", txtServerIP.Text);
cnd.SetAttribute("ServerPath", txtSerPath.Text);
cnd.SetAttribute("LocalPath", txtLocalPath.Text);
cnd.SetAttribute("UserName", txtUserName.Text);
cnd.SetAttribute("Password", txtPassword.Text);
cnd.SetAttribute("NetName", txtNeName.Text);
cnd.SetAttribute("TimeInterval", txtTimeInterval.Text);
node.AppendChild(cnd);
xmlDoc.Save("FtpHelp.xml");
xml文档中遍历查询节点
String neName = txtNeName.Text;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("FtpHelp.xml");
XmlNode node = xmlDoc.SelectSingleNode("ftpHelp");
foreach (XmlNode nd in node.ChildNodes)
{
if (nd.Attributes["NetName"].Value == neName)
{
MessageBox.Show("该网元已注册,注册信息为:","提示");
this.plNeInfo.Visible = true;
txtServerIP.Text = nd.Attributes["ServerIP"].Value;
txtSerPath.Text = nd.Attributes["ServerPath"].Value;
txtLocalPath.Text=nd.Attributes["LocalPath"].Value;
txtUserName.Text = nd.Attributes["UserName"].Value;
txtPassword.Text = nd.Attributes["Password"].Value;
txtTimeInterval.Text = nd.Attributes["TimeInterval"].Value;
this.btnAdd.Visible = false;
return;
}
}
MessageBox.Show("该网元不存在,请注册","提示");
private List<NeFtpInfo> GetXmlInfo(String fileName,List<String> neNameList)
{
if (neNameList!=null)
{
List<NeFtpInfo> neInList = new List<NeFtpInfo>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNode node = xmlDoc.SelectSingleNode("ftpHelp");
for (int i = 0; i < neNameList.Count; i++)
{
NeFtpInfo ni = new NeFtpInfo();
String neName = neNameList[i];
int j = 0;
for (j = 0; j < node.ChildNodes.Count; j++)
{
XmlNode nd = node.ChildNodes[j];
if (nd.Attributes["NetName"].Value == neName)
{
ni.NeName = neName;
ni.ServerIP = nd.Attributes["ServerIP"].Value;
ni.ServerPath = nd.Attributes["ServerPath"].Value;
ni.LocalPath = nd.Attributes["LocalPath"].Value;
ni.UserName = nd.Attributes["UserName"].Value;
ni.Password = nd.Attributes["Password"].Value;
ni.IntervalTime = nd.Attributes["TimeInterval"].Value;
neInList.Add(ni);
break;
}
}
if (j == node.ChildNodes.Count)
{
MessageBox.Show("网元" + neName + "没有注册过相关信息", "提示");
}
}
return neInList;
}
else
{
return null;
}
}
posted on 2008-09-25 15:41
天书 阅读(145)
评论(0) 编辑 收藏 引用