开发者

How to get all non empty nodes from XElement?

开发者 https://www.devze.com 2023-01-07 19:26 出处:网络
I\'m trying to get all nodes from an XElement that actually has a value, currently I\'m using this code:

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
 开发者_Go百科           select node;

Is there a build in operator to do this operation?

Thanks


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消