XDocument doc = XDocument.Parse(input); var nodes = doc.Element(rootNode).Descendants() .Where(n => (n.Value != "0" && n.Value != ".00" && n.Value != "false" && n.Value != "") || n.HasElements) .Select(n => new { n.Name, n.Value, Level = n.Ancestors().Count() - 1, n.HasElements, Descendants = n.Descendants().Count() }); var output = new StringBuilder(); foreach (var node in nodes) { if (node.HasElements) { output.AppendLine(new string(' ', node.Level) + node.Name.ToString() + ":"); if (0 == node.Descendants) output.AppendLine(""); } else { //else code here } } return output.ToString().TrimEnd('\r', '\n'); }My problem is that I want to insert an empty line in case my first element has only 1 empty child node element. I can not figure out how to make a check for this.