哪位大虾能提供一些用C#读写删改XML文件内容的资料啊?救小弟一命!! http://blog.csdn.net/lizanhong/archive/2004/06/23/24374.aspx希望对你有用。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private XmlDocument configData=new XmlDocument(); private string appPath = Application.ExecutablePath+".config";/// <summary> /// 获取节点值 /// </summary> /// <param name="key"></param> /// <returns></returns> private string GetAppValue(string key) { return configData.SelectSingleNode("/configuration/appSettings/add[@key='"+key+"']").Attributes["value"].Value; } /// <summary> /// 设置节点值 /// </summary> /// <param name="key"></param> /// <param name="newValue"></param> private void SetAppValue(string key,string newValue) { configData.SelectSingleNode("/configuration/appSettings/add[@key='"+key+"']").Attributes["value"].Value = newValue; configData.Save(appPath); }private void Init() { configData.Load(appPath);} TO 楼上各位大虾: 现在俺的想法是把KEY和value的值弄成dataset+dataGrid的方式,然后再进行添加删改。不过挂dataGrid时遇到麻烦,希望各位大虾指点迷津。分不够可再加。 给你一个例子:更改Web.config中的ConfigurationSettingpublic static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue) { try { string path=WebConfigDirectory+"\\web.config"; XmlDocument xd=new XmlDocument(); xd.Load(path); //如果没有appSetting,则添加 if(xd.SelectNodes("//appSettings").Count==0) { xd.DocumentElement.AppendChild(xd.CreateElement("appSettings")); } //判断节点是否存在,如果存在则修改当前节点 bool addNode=true; foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add")) { if(xn1.Attributes["key"].Value==appSettingsAddkey) { addNode=false; xn1.Attributes["value"].Value=keyvalue; //xn1.ParentNode.RemoveChild(xn1); break; } } if(addNode) { //创建新节点 XmlNode xn2=xd.CreateElement("add"); //添加key XmlAttribute xa=xd.CreateAttribute("key"); xa.Value=appSettingsAddkey; xn2.Attributes.Append(xa); //添加value xa=xd.CreateAttribute("value"); xa.Value=keyvalue; xn2.Attributes.Append(xa); xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2); } //保存web.config xd.Save(path); return true; } catch { return false; } } CSDN和MSDN上都有例子,楼主不妨看一下. http://cn.thespoke.net/MyBlog/xrascal/MyBlog.aspx 现在的主要问题是如何通过dataset挂到dataGrid上,并进行增、删、改。希望高手们来指点指点。 关于winform无边框窗体圆角功能,最大化时想不圆角,只有normal时圆角 网页上flash的http请求中乱码的问题 【牛人解答】DateTable中Compute,列Expression 问题,困恼 如何修改c#中 button控件的形状 求一段正则表达 如何替换后保留原有数字 c#中一个复杂的数据库操作 请各位帮帮忙,C#实现不同数据库数据导入 各位有没有什么好的表格控件,dotnet下的,推荐一下! 求几条正则表达式的含义,顺带求正则表达式资料下载!! 100分求 端口扫描的源代码 C#中如何表示函数指针? C#里用不了指针吧?
private string appPath = Application.ExecutablePath+".config";/// <summary>
/// 获取节点值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private string GetAppValue(string key)
{
return configData.SelectSingleNode("/configuration/appSettings/add[@key='"+key+"']").Attributes["value"].Value;
}
/// <summary>
/// 设置节点值
/// </summary>
/// <param name="key"></param>
/// <param name="newValue"></param>
private void SetAppValue(string key,string newValue)
{
configData.SelectSingleNode("/configuration/appSettings/add[@key='"+key+"']").Attributes["value"].Value = newValue;
configData.Save(appPath);
}private void Init()
{
configData.Load(appPath);
}
public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue)
{
try
{
string path=WebConfigDirectory+"\\web.config";
XmlDocument xd=new XmlDocument();
xd.Load(path);
//如果没有appSetting,则添加
if(xd.SelectNodes("//appSettings").Count==0)
{
xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
}
//判断节点是否存在,如果存在则修改当前节点
bool addNode=true;
foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
{
if(xn1.Attributes["key"].Value==appSettingsAddkey)
{
addNode=false;
xn1.Attributes["value"].Value=keyvalue;
//xn1.ParentNode.RemoveChild(xn1);
break;
}
}
if(addNode)
{
//创建新节点
XmlNode xn2=xd.CreateElement("add");
//添加key
XmlAttribute xa=xd.CreateAttribute("key");
xa.Value=appSettingsAddkey;
xn2.Attributes.Append(xa);
//添加value
xa=xd.CreateAttribute("value");
xa.Value=keyvalue;
xn2.Attributes.Append(xa);
xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
}
//保存web.config
xd.Save(path);
return true;
}
catch
{
return false;
}
}