public static string XmlReader(string strName)
{
string strValue = "";
string typeName = "";
FileStream myfile = new FileStream(getFilePath("Config.xml"), FileMode.Open);
XmlTextReader xmlTextReader = new XmlTextReader(myfile);
if (null == xmlTextReader)
{
throw new Exception("找不到配置文件!");
}
xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;
try
{
while (xmlTextReader.Read())
{
switch (xmlTextReader.NodeType)
{
case XmlNodeType.Element :
typeName = xmlTextReader.Name;
break;
case XmlNodeType.Text :
if (typeName == strName)
{
strValue = xmlTextReader.Value;
}
break;
}
if (strValue != "")
break;
}
return strValue;
}
finally
{
if (myfile != null)
myfile.Close();
if (xmlTextReader != null)
xmlTextReader.Close();
}
}
说明:如果用XmlTextReader这个读取XML,实际上是比较麻烦,因为这个不是一个节点一个节点的读的。所以可以看到我这边是采用红色的那段代码来处理的。