0
\$\begingroup\$

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();
asked Aug 6, 2014 at 7:23
\$\endgroup\$
2
  • \$\begingroup\$ What is not "neat" about your approach? \$\endgroup\$ Commented Aug 6, 2014 at 7:34
  • \$\begingroup\$ @derape The looping. I'd prefer a functional not declarative approach like LINQ or such. \$\endgroup\$ Commented Aug 6, 2014 at 20:59

1 Answer 1

4
\$\begingroup\$

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();
svick
24.5k4 gold badges53 silver badges89 bronze badges
answered Aug 6, 2014 at 7:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.