Arjor
Arjor

Reputation: 1049

How to get all non empty nodes from XElement?

I'm trying to get all nodes from an XElement that actually has a value, currently I'm using this code:

var nodes = from node in elem.Nodes()
            where node.NodeType == XmlNodeType.Element &&
                  ((XElement) node).Value.Length >  0
            select node;

Is there a build in operator to do this operation?

Thanks

Upvotes: 1

Views: 731

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500514

I don't believe there's anything like this built in. Are you sure you want to include elements that have subelements though? For example:

XElement e = new XElement("Foo", new XElement("Bar"));
Console.WriteLine(e);
Console.WriteLine(e.Value.Length);

This will print:

<Foo>
  <Bar />
</Foo>
0

... so Foo would be included as an "empty" node even though it contains another element. Is that definitely what you're after?

Upvotes: 2

Related Questions