从从列表框C#中选择的xml文件中删除节点

时间:2018-08-04 15:22:43

标签: c# visual-studio

我正在尝试删除一个节点,在一个xml文件中,其名称由一个列表框指定(列表框中仅显示每个设置的,我需要删除该名称下的每个设置),一切,但我不能使其工作。这是我尝试的列表:

private void dlt_btn_Click(object sender, EventArgs e)
    {

        string selectednode = Convert.ToString(listBox1.SelectedItem);

        XmlDocument doc = new XmlDocument();
        doc.Load("dati.txt");

        XmlNodeList nodes = doc.GetElementsByTagName(selectednode);
        XmlNode node = nodes[0];
        node.ParentNode.RemoveChild(node);

        doc.Save("dati.txt");
    }

这是xml文件:

<?xml version="1.0"?>
<DATA>
  <name>wewe4</name>
  <SETTING_LIST>
    <setting>
      <name>Ping</name>
      <enable>config</enable>
      <disable>config</disable>
      <check>config</check>
      <word>ping</word>
      <ifchecktrue>true</ifchecktrue>
      <usegrep>true</usegrep>
    </setting>
    <setting>
      <name>Pong</name>
      <enable>config</enable>
      <disable>config</disable>
      <check>config</check>
      <word>ping</word>
      <ifchecktrue>true</ifchecktrue>
      <usegrep>true</usegrep>
    </setting>
    </DATA>

1 个答案:

答案 0 :(得分:0)

如果我了解您,这就是您使用<!DOCTYPE html> <html> <head> </head> <body> <h2>Dropdown Image</h2> <p>Move the mouse over the image below to open the dropdown content.</p> <div class="dropdown"> <img id="image1" src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="100" height="50"> <div class="dropdown-content"> <img src="https://www.w3schools.com/css/img_5terre.jpg" alt="Cinque Terre" width="300" height="200"> <div class="desc">Beautiful Cinque Terre</div> </div> </div> </body> </html>的方式,此代码不使用System.Xml.Linq.XDocument XmlDocument是较新的API:

XDocument