我想吧
<?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>请教!

解决方案 »

  1.   

    http://topic.csdn.net/u/20110211/16/8e64d69b-5b98-4342-8fd5-f8c7d02e7e9e.html
    看这个
      

  2.   

    http://blog.csdn.net/DaiDaiLiu/archive/2011/03/01/6215195.aspx
    希望能对你有帮助
      

  3.   

    在Foreach里删元素是要出问题的,用For(;;)试试
      

  4.   

    另外,你想要删掉的是xe,而xe.RemoveAll()删掉的是啥子?
      

  5.   

    你可以在<system.web>这一级的XmlNode删除.RemoveChild(.....)
      

  6.   

    XmlDocument doc = new XmlDocument();
      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);
      

  7.   

    xml 删除
    linq to xml  删除
      

  8.   


    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);
           }
      }
      

  9.   

    可以试下,xmlNode.RemoveChild(xe);
    这样应该是没问题的。xe.RemoveAll();只是删除了他下面的所有东西,并不包过删除他自己