wince xml 配置文件设置,直接上源码

前端之家收集整理的这篇文章主要介绍了wince xml 配置文件设置,直接上源码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

public static class ConfigManager
{
private static string configName="app.xml";
private static string XMLPath = "";
private static XmlDocument xmlIAUConfig;
public static XmlDocument XMLIAUConfig
{
get { return xmlIAUConfig; }
set { xmlIAUConfig = value; }
}
private static string platForm;
private static string PlatForm
{
get
{
return Environment.OSVersion.Platform.ToString();
}
}
private static string currentPath;
public static string CurrentPath
{
get
{
if (PlatForm.Equals("WinCE"))
{
currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
}
else if (PlatForm.Equals("Win32NT"))
{
currentPath = Directory.GetCurrentDirectory();
}

return currentPath;
}
}

static ConfigManager()
{
xmlIAUConfig = new XmlDocument();
XMLPath = CurrentPath;
Int32 i = XMLPath.LastIndexOf("/");
if (i >= 0)
XMLPath = XMLPath.Remove(i);
XMLPath = XMLPath + "//"+configName;
xmlIAUConfig.Load(XMLPath);
}

public static String GetValue(String key)
{
xmlIAUConfig.Load(XMLPath);
String value;
String path = @"//DataAccess/appSettings/add[@key='" + key + "']";
XmlNodeList xmlAdds = xmlIAUConfig.SelectNodes(path);

if (xmlAdds.Count == 1)
{
XmlElement xmlAdd = (XmlElement)xmlAdds[0];

value = xmlAdd.GetAttribute("value");
}
else
{
throw new Exception("IAUConfig配置信息设置错误:键值为" + key + "的元素不等于1");
}

return value;
}
public static void SavaConfig(string strKey,string strValue)
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(configName);
XmlNodeList list = XMLDoc.GetElementsByTagName("add");

for (int i = 0; i < list.Count; i++) { if (list[i].Attributes[0].Value == strKey) { list[i].Attributes[1].Value = strValue; } } StreamWriter swriter = new StreamWriter(configName); XmlTextWriter xw = new XmlTextWriter(swriter); xw.Formatting = Formatting.Indented; XMLDoc.WriteTo(xw); xw.Close(); swriter.Close(); } }

原文链接:https://www.f2er.com/xml/299607.html

猜你在找的XML相关文章