读xml文件中的某一个参数的值,也要分析整个xml文件,没有可以直接指定读某个值的,建义你可以将这个xml的文件先读取程序中用数组或其它的方法保存起来,并建立一种索引机制就行了,至于读xml的方法,你可以参考下面这段程序:
public XMLOperate(string xmlFileName, string nodeRootsName)
{
#region 文件读取,并看文件是否符合要求
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch (System.Exception e1)
{
this.err = "读取文件失败!\n"+e1.Message;
return;
}
System.Xml.XmlNodeList nodeRoots = doc.GetElementsByTagName(nodeRootsName);//与之相配的节点数
if (nodeRoots.Count == 0)
{
string sErr = "格式不对,请用标准的格式文件编写!\n";
err = sErr;
return;
}
#endregion 文件读取完成
#region 读取文件的分类
System.Xml.XmlNode nodeRoot = nodeRoots[0];//第一级子目录
try
{
for (int i = 0; i < nodeRoot.ChildNodes.Count; i++)
{
System.Xml.XmlNode nodeSecond = nodeRoot.ChildNodes[i];//第二级目录
if (nodeSecond.NodeType == System.Xml.XmlNodeType.Element) //类型
{
switch (nodeSecond.Name.ToLower().Trim())//取相应的名字
{
case "name1":
//测试用
this.name = nodeSecond.Attributes.GetNamedItem("name").Value.ToString();
this.dbIndex = int.Parse(nodeSecond.Attributes.GetNamedItem("Index").Value.ToString());
this.dbdCount = int.Parse(nodeSecond.Attributes.GetNamedItem("Count").Value.ToString());
//测试完
this.blockReads.Add(new BlockRead(nodeSecond));
break;
case "name2":
this.name = nodeSecond.Attributes.GetNamedItem("name").Value.ToString();
this.dbIndex = int.Parse(nodeSecond.Attributes.GetNamedItem("Index").Value.ToString());
this.dbdCount = int.Parse(nodeSecond.Attributes.GetNamedItem("Count").Value.ToString());
this.blockWrites.Add(new BlockWrite(nodeSecond));
break;
default:
break;
}
}
}
}