一、配置文件示例
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>
二、调用示例
public void Debug()
{
string version = "";//此应用程序支持的运行库版本
string sku = "";//此应用程序支持的 .NET Framework 版本
version = myConfig.GetAttribute("configuration/startup/supportedRuntime", "version");
sku = myConfig.GetAttribute("configuration/startup/supportedRuntime", "sku").GetValue();
// version="v4.0"
// sku="v4.5.1"
}
三、扩展方法
namespace System
{
public static class xzSystem
{
/// <summary>
/// <format>name=value</format>
/// </summary>
/// <param name="src"></param>
/// <param name="linkChar"></param>
/// <returns></returns>
public static string GetName(this string src, string linkChar = "=")
{
string ret = "";
try
{
if (string.IsNullOrEmpty(src)) return src;
if (src.Contains(linkChar) == false) return src;
int index = src.IndexOf(linkChar);
if (index < 0) return src;
else return src.Substring(0, index);
}
catch (Exception)
{
}
return ret;
}
/// <summary>
/// <format>name=value</format>
/// </summary>
/// <param name="src"></param>
/// <param name="linkChar"></param>
/// <returns></returns>
public static string GetValue(this string src, string linkChar = "=")
{
string ret = "";
try
{
if (string.IsNullOrEmpty(src)) return src;
if (src.Contains(linkChar) == false) return src;
int index = src.IndexOf(linkChar);
if (index < 0) return src;
else return src.Substring(index + linkChar.Length, src.Length - index - linkChar.Length);
}
catch (Exception)
{
}
return ret;
}
}
}
四、自定义实现方法
namespace xzSpace
{
public class myConfig
{
/// <summary>
/// 获取指定节点所有属性
/// </summary>
/// <param name="xpath">指定节点路径</param>
/// <param name="attributeName">指定属性名称</param>
/// <returns></returns>
public static System.Collections.Generic.List<string> GetAttributes(string xpath = "configuration/startup/supportedRuntime", string attributeName = "sku")
{
System.Collections.Generic.List<string> ret = new System.Collections.Generic.List<string>();
try
{
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(config.FilePath);
//root = doc.DocumentElement;
System.Xml.XmlNodeList listNodes = null;
listNodes = doc.SelectNodes(xpath);
foreach (System.Xml.XmlNode node in listNodes)
{
if (node == null) continue;
if (node.GetType() != typeof(System.Xml.XmlElement)) continue;
foreach (System.Xml.XmlAttribute xa in node.Attributes)
{
if (xa.Name == attributeName)
{
ret.Add(xa.Value);
}
}
}
doc.RemoveAll();
}
catch (System.Exception)
{
}
return ret;
}
/// <summary>
/// 获取指定节点属性
/// </summary>
/// <param name="xpath">指定节点路径</param>
/// <param name="attributeName">指定节点属性</param>
/// <returns></returns>
public static string GetAttribute(string xpath = "configuration/startup/supportedRuntime", string attributeName = "sku")
{
string ret = null;
try
{
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(config.FilePath);
System.Xml.XmlNodeList listNodes = null;
listNodes = doc.SelectNodes(xpath);
foreach (System.Xml.XmlNode node in listNodes)
{
if (node == null) continue;
if (node.GetType() != typeof(System.Xml.XmlElement)) continue;
foreach (System.Xml.XmlAttribute xa in node.Attributes)
{
if (xa.Name == attributeName)
{
ret=xa.Value;
}
}
}
doc.RemoveAll();
}
catch (System.Exception)
{
}
return ret;
}
}
}