C#如何修改以.config结尾的XML文件
RT~xml文件名为app.configxml文件内容为<?xmlversion="1.0"encoding="utf-8"?><configuration><syste...
RT~
xml文件名为app.config
xml文件内容为
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="BindingBehaviorConfiguration">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.1.101:1234/MessageService" binding="netTcpBinding" bindingConfiguration="BindingBehaviorConfiguration" contract="ServiceReference1.IMessageService" name="NetTcpBinding_IMessageService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
如何通过C#的文件操作实现对其中IP的修改?
如何使用Document.SelectSingleNode方法呢?以下段落总是显示无效
XmlNode node = Document.SelectSingleNode("configuration/system.serviceModel/client[endpoint address = " + "\"" + "net.tcp://192.169.1.101:1234/MessageService" + " binding=" + "\"" + "netTcpBinding" + "\"" + "bindingConfiguration=" + "\"" + "BindingBehaviorConfiguration" + "\"" + "contract=" + "\"" + "ServiceReference1.IMessageService" + "\"" + " name=" + "\"" + "NetTcpBinding_IMessageService" + "\"" + "]");
如何清空一个节点内的全部内容,并给予新的内容? 展开
xml文件名为app.config
xml文件内容为
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="BindingBehaviorConfiguration">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://192.168.1.101:1234/MessageService" binding="netTcpBinding" bindingConfiguration="BindingBehaviorConfiguration" contract="ServiceReference1.IMessageService" name="NetTcpBinding_IMessageService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
如何通过C#的文件操作实现对其中IP的修改?
如何使用Document.SelectSingleNode方法呢?以下段落总是显示无效
XmlNode node = Document.SelectSingleNode("configuration/system.serviceModel/client[endpoint address = " + "\"" + "net.tcp://192.169.1.101:1234/MessageService" + " binding=" + "\"" + "netTcpBinding" + "\"" + "bindingConfiguration=" + "\"" + "BindingBehaviorConfiguration" + "\"" + "contract=" + "\"" + "ServiceReference1.IMessageService" + "\"" + " name=" + "\"" + "NetTcpBinding_IMessageService" + "\"" + "]");
如何清空一个节点内的全部内容,并给予新的内容? 展开
3个回答
展开全部
app.config文件为应用程序的配置文件,操作此文件并不能按操作一般的XML文件那样,而是需要C#提供的关于操作配置文件的类库Configuration来操作.另外还要添加此应用(System.Configuration)才行:在vs右边的文件列表里解决方案上右击,选中添加引用,在弹出的对话框里选中System.Configuration添加即可.对于配置文件的修改,有些麻烦.下面的代码希望能给你一点帮助: Configuration config=ConfigurationManager.OpenExeConfiguration("D:\\yeyun\\MyQQ\\MyQQ\\app.config");
AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
if (appSetting.Settings["key"] == null)
{
appSetting.Settings.Add("key", "net.tcp://192.168.1.101:1234");
}
else
{
appSetting.Settings["key"].Value = "hao";
}
ConnectionStringsSection conns = (ConnectionStringsSection)config.GetSection("connectionStrings");
if (conns.ConnectionStrings["name"] == null)
{
ConnectionStringSettings conn = new ConnectionStringSettings("name","dddd");
conns.ConnectionStrings.Add(conn);
}
config.Save();
上面分别是修改appSettings节点和connectionStrings节点
但是我没有找到system.serviceModel这个节点,我用的是vs2005。不知道你用的是什么版本
注:该配置文件不建议在程序运行时修改...
AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
if (appSetting.Settings["key"] == null)
{
appSetting.Settings.Add("key", "net.tcp://192.168.1.101:1234");
}
else
{
appSetting.Settings["key"].Value = "hao";
}
ConnectionStringsSection conns = (ConnectionStringsSection)config.GetSection("connectionStrings");
if (conns.ConnectionStrings["name"] == null)
{
ConnectionStringSettings conn = new ConnectionStringSettings("name","dddd");
conns.ConnectionStrings.Add(conn);
}
config.Save();
上面分别是修改appSettings节点和connectionStrings节点
但是我没有找到system.serviceModel这个节点,我用的是vs2005。不知道你用的是什么版本
注:该配置文件不建议在程序运行时修改...
展开全部
很复杂啊。好运吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先把后缀名改为.xml
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load("你转换的");
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load("你转换的");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询