如何获取这样的XML节点 本帖最后由 dillon00072002 于 2014-09-26 11:09:19 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 获得XXX么?我有一个xml是这样的<configuration> <appSettings> <add key="ServerIP" value="192.168.1.120" /> <add key="ListenPort" value="5555" /> <add key="MaxSize" value="1024" /> <add key="MaxCon" value="20" /> </appSettings></configuration>我获取某个节点的value是使用了 XmlDocument doc = new XmlDocument();doc.Load(Environment.CurrentDirectory + "/app.config"); XmlNodeList nodes = doc.GetElementsByTagName("add");var ServerIP = nodes.Cast<XmlNode>().Where(d => d.Attributes["key"].Value == "ServerIP").First().Attributes["value"].Value; 你的应该大同小异 改下就可以用了 不过不是value应该是节点的text或者什么的吧 是获取最后一行property里边的数据库连接信息 你这个是配置文件?直接可以这样获取啊ConfigurationManager.AppSettings["MaxCon"] 我当然知道 不过我看到xxx.exe.config 我就闹心... 我代码上有洁癖 看不得这样的东西..还不能把exe去掉..所以我只能自己添加一个假"配置文件" 自己完成读写操作了. 我当然知道 不过我看到xxx.exe.config 我就闹心... 我代码上有洁癖 看不得这样的东西..还不能把exe去掉..所以我只能自己添加一个假"配置文件" 自己完成读写操作了.刚才用的办法解决了,现在又出了一个问题,我怎么样才能修改这个字段里边的东西。。 public static void SaveConfig(string ConnenctionString, string strKey) { XmlDocument doc = new XmlDocument(); //获得配置文件的全路径 string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; doc.Load(strFileName); //找出名称为“add”的所有元素 XmlNodeList nodes = doc.GetElementsByTagName("add"); for (int i = 0; i < nodes.Count; i++) { //获得将当前元素的key属性 XmlAttribute att = nodes[i].Attributes["key"]; //根据元素的第一个属性来判断当前的元素是不是目标元素 if (att.Value == strKey) { //对目标元素中的第二个属性赋值 att = nodes[i].Attributes["value"]; att.Value = ConnenctionString; break; } } //保存上面的修改 doc.Save(strFileName); } 当然了 这也是根据我的config来配置的 具体里面的节点 你可以自行修改 序列化与反序列化 C#的word程序,“应用程序级项目”、“文档级项目”、“外接程序项目”有何区别 请问如何控制一个进程(或线程)的CPU占用率和内存使用率 C# 字符串切分问题 有字符串jaf/dfadasf aa adfdasad分三段,如何只得到最后的这串字符adfdasad? 敢问那位大侠这是怎么回事? 如何判断label是否为空 求救.学习时遇到的问题:xml插不进Sql 2005数据库 水晶报表打印的时候怎样才能不弹出打印设置的对话框?直接打印!急! c#和vc++有什么区别? C#会对于未赋值的变量/成员变量,给予一个初始值吗? 请问 == 和 值类型有什么关系呀?
我有一个xml是这样的
<configuration>
<appSettings>
<add key="ServerIP" value="192.168.1.120" />
<add key="ListenPort" value="5555" />
<add key="MaxSize" value="1024" />
<add key="MaxCon" value="20" />
</appSettings>
</configuration>
我获取某个节点的value是使用了 XmlDocument doc = new XmlDocument();
doc.Load(Environment.CurrentDirectory + "/app.config");
XmlNodeList nodes = doc.GetElementsByTagName("add");
var ServerIP = nodes.Cast<XmlNode>().Where(d => d.Attributes["key"].Value == "ServerIP").First().Attributes["value"].Value;
你这个是配置文件?直接可以这样获取啊
ConfigurationManager.AppSettings["MaxCon"]
我当然知道 不过我看到xxx.exe.config 我就闹心... 我代码上有洁癖 看不得这样的东西..还不能把exe去掉..所以我只能自己添加一个假"配置文件" 自己完成读写操作了.
我当然知道 不过我看到xxx.exe.config 我就闹心... 我代码上有洁癖 看不得这样的东西..还不能把exe去掉..所以我只能自己添加一个假"配置文件" 自己完成读写操作了.
刚才用的办法解决了,现在又出了一个问题,我怎么样才能修改这个字段里边的东西。。
public static void SaveConfig(string ConnenctionString, string strKey)
{
XmlDocument doc = new XmlDocument();
//获得配置文件的全路径
string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = ConnenctionString;
break;
}
}
//保存上面的修改
doc.Save(strFileName);
}