How to remove an xml element from file?

后端 未结 3 480
攒了一身酷
攒了一身酷 2020-11-29 12:01

In an XML file such as :


 
   
   code goes here
   
 

          


        
3条回答
  •  广开言路
    2020-11-29 12:53

    XDocument doc = XDocument.Load("input.xml");
    var q = from node in doc.Descendants("Snippet")
        let attr = node.Attribute("name")
        where attr != null && attr.Value == "abc"
        select node;
    q.ToList().ForEach(x => x.Remove());
    doc.Save("output.xml");
    

    .Net 2.0

    XmlDocument doc = new XmlDocument();
    doc.Load("input.xml");
    XmlNodeList nodes = doc.SelectNodes("//Snippet[@name='abc']");
    

    Now you have the nodes whose attribute name='abc', you can now loop through it and delete

提交回复
热议问题