Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to check if the Node has only empty descendants
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
XML
Title:
How to check if the Node has only empty descendants
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01567676
Message ID:
01567676
Views:
41
Hi everybody,

I have the following code:
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.

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform