我想吧
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1" timeout="50"/>
</system.web>
</configuration>
这里的<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1" timeout="50"/>节点删除
用了 XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList xml = doc.SelectSingleNode("//configuration/system.web").ChildNodes;
foreach (var item in xml)
{
XmlElement xe = item as XmlElement;
if (xe.GetAttribute("mode") == "StateServer")
{
//(xe as XmlNode).RemoveAll();
xe.RemoveAll();
}
}
doc.Save(path);
不行,只删除了它的属性,结果是
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<sessionState />
</system.web>
</configuration>我想要的结果是<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web> </system.web>
</configuration>请教!
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1" timeout="50"/>
</system.web>
</configuration>
这里的<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1" timeout="50"/>节点删除
用了 XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList xml = doc.SelectSingleNode("//configuration/system.web").ChildNodes;
foreach (var item in xml)
{
XmlElement xe = item as XmlElement;
if (xe.GetAttribute("mode") == "StateServer")
{
//(xe as XmlNode).RemoveAll();
xe.RemoveAll();
}
}
doc.Save(path);
不行,只删除了它的属性,结果是
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<sessionState />
</system.web>
</configuration>我想要的结果是<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web> </system.web>
</configuration>请教!
解决方案 »
- 添加一个角色前是覆盖还是新增问题
- 急!VS2008开发WinCE5.0程序PictureBox.Image加载图片时出错
- XML文件解析
- 如何用C#在一个字符串中以回车为分隔标志分段读取?
- 当我第2次点击这个BUTTON,程序未响应了
- 做象word的启动欢迎界面,怎么程序要等主窗口打开才能把欢迎界面显示出来! 急,谢谢!
- 急!急! 怎么得到checkedListBox控件中 没有选中的项目的 数据?急,在线等待!
- 新手跪求指点:WPF的TREEVIEW能否实现以下的需求?
- 编译非安全代码的问题
- 关于c#webbrowser获取不到无ID元素
- BackgroundWorker用法的一点疑惑,求解答!
- 在C#中实现TEXTBOX或者COMBOBOX控件下拉功能
看这个
希望能对你有帮助
doc.Load(path);
XmlNode xmlNode = doc.SelectSingleNode("//configuration/system.web");
XmlNodeList xml = doc.SelectSingleNode("//configuration/system.web").ChildNodes;
foreach (var item in xml)
{
XmlElement xe = item as XmlElement;
if (xe.GetAttribute("mode") == "StateServer")
{
xmlNode.RemoveChild(xe);
}
}
doc.Save(path);
linq to xml 删除
XmlNodeList xml = doc.SelectSingleNode("//configuration/system.web").ChildNodes;
XmlNode xmlNode = doc.SelectSingleNode("//configuration/system.web"); foreach (var item in xml)
{
XmlElement xe = item as XmlElement;
if (xe.GetAttribute("mode") == "StateServer")
{
xmlNode.RemoveChild(xe);
}
}
这样应该是没问题的。xe.RemoveAll();只是删除了他下面的所有东西,并不包过删除他自己