Remove empty tags from xml doc


Remove empty tags

Remove empty tags from xml document




By using regular expression we can remove empty tags from xml document.

 
Regex regex = new Regex(@"(\s)*<(\w:\w|\w)*(\s)*/>");
string xml= regex.Replace(xml, string.Empty);





We can also use LINQ to XML as follows


XDocument doc = XDocument.Parse(xml);
doc.Descendants().Where(d => !d.DescendantNodes().OfType().Any(t => t.Value.Trim() != "")).Remove();
return doc.ToString();

xmls is xml string


Comments

Author: Nikhil19 Apr 2010 Member Level: Gold   Points : 1

Hi Blessy, nice post.
you can also refer to my post at
http://www.dotnetspider.com/resources/37029-How-remove-all-HTML-Tags-from-specified-string.aspx.
This shows how to remove all HTML tags using Regex.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: