积极答复者
请帮忙看下这个代码应怎么写?

问题
-
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appUserset>
<add key="user" value="" />
</appUserset>
<configDate>
<add key="sePath" value="" />
<add key="sePort" value="" />
<add key="seName" value="" />
<add key="sePass" value="" />
</configDate>
</configuration>我想查找并修改对应key值的value值!
不知道代码应怎么写,请高人指点!
答案
-
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appUserset>
<add key="user" value="" />
</appUserset>
<configDate>
<add key="sePath" value="" />
<add key="sePort" value="" />
<add key="seName" value="" />
<add key="sePass" value="" />
</configDate>
</configuration>我想查找并修改对应key值的value值!
不知道代码应怎么写,请高人指点!
XDocument doc = XDocument.Load("xxx.xml");var result = (from e in doc.Descedants("add")
where e.Attribute("key").Value.Equals("要查找的内容")
select e).First();
result.Attribute("value").Value = new value;
doc.Save("C:\\try.xml");
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 zyj_keyen 2011年7月4日 23:44
- 已编辑 ThankfulHeartModerator 2011年7月6日 7:59
-
XmlDocument upDoc = new XmlDocument();
string strFilePath = Application.ExecutablePath.ToString() + ".config"; //获得配置文件的全路径
XDocument TempDoc = XDocument.Load(strFilePath);找到答案了,多声明了一个,保存时保存了个空的。upDoc.Save("AttendanceClient.exe.config");//保存。哈哈!
- 已标记为答案 zyj_keyen 2011年7月7日 2:49
全部回复
-
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appUserset>
<add key="user" value="" />
</appUserset>
<configDate>
<add key="sePath" value="" />
<add key="sePort" value="" />
<add key="seName" value="" />
<add key="sePass" value="" />
</configDate>
</configuration>我想查找并修改对应key值的value值!
不知道代码应怎么写,请高人指点!
XDocument doc = XDocument.Load("xxx.xml");var result = (from e in doc.Descedants("add")
where e.Attribute("key").Value.Equals("要查找的内容")
select e).First();
result.Attribute("value").Value = new value;
doc.Save("C:\\try.xml");
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已标记为答案 zyj_keyen 2011年7月4日 23:44
- 已编辑 ThankfulHeartModerator 2011年7月6日 7:59
-
private void upConfig()
{
XmlDocument upDoc = new XmlDocument();
string strFilePath = Application.ExecutablePath.ToString() + ".config"; //获得配置文件的全路径
XDocument TempDoc = XDocument.Load(strFilePath);var result = (from e in TempDoc.Descendants("add")
where e.Attribute("key").Value.Equals("要查找的内容")
select e).First();
e.Attribute("value").Value = new value; //当前上下文中不存名称“e”。未能找到类型或命名空间名称“value”是否缺少using引用或程序集。new value是不是直接是值了,如"值"//new表达式要求在类型后有()、{}或[] 请指点!谢谢!
upDoc.Save("AttendanceClient.exe.config");//保存。
}
-
请认真价差你的result是不是null?
另外,你理解错误我的意思了——
e.Attribute("value").Value = 这里用字符串写入你要的东西!
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处 -
private void upConfig()
{
XmlDocument upDoc = new XmlDocument();
string strFilePath = Application.ExecutablePath.ToString() + ".config"; //获得配置文件的全路径
XDocument TempDoc = XDocument.Load(strFilePath);var result = (from e in TempDoc.Descendants("add")
where e.Attribute("key").Value.Equals("seName")
select e).First();
e.Attribute("value").Value = "123"; //改了后提示:当前上下文中不存在名称“e”
upDoc.Save("AttendanceClient.exe.config");//保存。
}我改成这样:
private void upConfig()
{
XmlDocument upDoc = new XmlDocument();
string strFilePath = Application.ExecutablePath.ToString() + ".config"; //获得配置文件的全路径
XDocument TempDoc = XDocument.Load(strFilePath);var result = (from e in TempDoc.Descendants("add")
where e.Attribute("key").Value.Equals("seName")
select e).First();
result.Attribute("value").Value = "123"; //运行是行了,但报错,说XML文档无效,没有根元素。请高手指点!
upDoc.Save("AttendanceClient.exe.config");//保存。
}
-
楼主你好:)
可以给我看看你xml文档是什么样子的?我来自己动手做做
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处 -
XmlDocument upDoc = new XmlDocument();
string strFilePath = Application.ExecutablePath.ToString() + ".config"; //获得配置文件的全路径
XDocument TempDoc = XDocument.Load(strFilePath);找到答案了,多声明了一个,保存时保存了个空的。upDoc.Save("AttendanceClient.exe.config");//保存。哈哈!
- 已标记为答案 zyj_keyen 2011年7月7日 2:49
-
恩,hehe
请把自己也标记成答案。
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请
If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处