-1

I would like to remove tag like the following one with its attributes using C# .Net how can i do it?

<aaa type="1" class="2" />

other tags like <bbb type="5" class="4" /> i would like to keep.

Best Regards,

John
  • 1
  • Remove it from what? How are you getting the XML document? Show us some code, so we can figure out what you are talking about. – Oded May 09 '10 at 08:28
  • You can't, not robustly. XML is not a regular language, and cannot be reliably parsed with regular expressions. http://stackoverflow.com/questions/1875258/regular-expression-to-parse-links-from-html-code/1875281#1875281 – Michael Petrotta May 09 '10 at 08:29
  • replacing the with empty string – John May 09 '10 at 08:29
  • How do you identify which tags you want to remove and which you wish to keep? – Mark Byers May 09 '10 at 08:30
  • i know them for advance. i can do it with alot of regexs each one for each tag... i don't care... – John May 09 '10 at 08:31
  • @John: But if you don't tell us how to identify which tags you want to remove, it's very difficult to give a specific answer. I can only give a starting point. – Mark Byers May 09 '10 at 08:35

1 Answers1

2

I'd advise against regular expressions for this task.

However you can use LINQ to XML to remove tags with name "aaa" like this:

XDocument doc = XDocument.Load("input.xml");
doc.Descendants("aaa").Remove();
doc.Save("output.xml");
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452