读取XML文件中获取特定值
Posted yuesebote
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取XML文件中获取特定值相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 获取子系统驱动获取DCM配置文件的特定值
/// </summary>
/// <param name="configurationFile"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string ReadValueByKey(ConfigurationFile configurationFile, string key)
{
string value = string.Empty;
string filename = string.Empty;
if (configurationFile.ToString() == ConfigurationFile.AppXml.ToString())
{
//string exeroot = "F:\\DCM\\DLAX.DataCollectorMiddleware\\DLAX.DataCollectorMiddleware\\bin\\Debug\\DeviceLib\\视频子系统\\北京电科院\\BJDKY\\Start.exe";
string exeroot = System.Windows.Forms.Application.ExecutablePath;
if (exeroot.IndexOf("DeviceLib") > 0)
{
string fileroot = exeroot.Substring(0, exeroot.IndexOf("DeviceLib"));
filename = fileroot + "dcm.xml";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "dcm.xml";
}
}
XmlDocument doc = new XmlDocument();
try
{
doc.Load(filename); //加载配置文件
}
catch (Exception ex)
{
return null;
}
XmlNode node = doc.SelectSingleNode("//appSettings"); //得到[appSettings]节点
////得到[appSettings]节点中关于Key的子节点
XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key=‘" + key + "‘]");
if (element != null)
{
value = element.GetAttribute("value");
}
return value;
}
以上是关于读取XML文件中获取特定值的主要内容,如果未能解决你的问题,请参考以下文章