Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In this reply, there's an example of how to remove a node from a XML structure.

XElement doc = XElement.Load("test.xml");
doc.XPathSelectElement("//beep").Remove();

Suppose now that we wish to remove not a single node (specifying it by the string value) but a set of nodes (specifying them by an array of strings).

One way to resolve it is to loop through the array and execute the removal for each string element. However, I'd like to know if someone can suggest a neater approach.

IEnumerable<String> beeps = new[] { "//beep" };
XElement doc = XElement.Load("test.xml");
foreach(String beep in beeps)
  doc.XPathSelectElement(beep).Remove();
share|improve this question
    
What is not "neat" about your approach? – derape Aug 6 '14 at 7:34
    
@derape The looping. I'd prefer a functional not declarative approach like LINQ or such. – Konrad Viltersten Aug 6 '14 at 20:59
up vote 4 down vote accepted

Let us say you have a node like this which is having two nodes , node1 and node 2, you need to specify xpath using "|" seperated . here is an example .

XPathSelectElement should be replaced with XPathSelectElements.

<?xml version="1.0" encoding="utf-8" ?>
<customer>
  <node1>
  </node1>
  <node2>
  </node2>
</customer>
//all you need sepcify is a "|" to work it out.
doc.XPathSelectElements("//node1 | //node2").Remove();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.