【发布时间】:2011-11-17 15:37:15
【问题描述】:
我想知道是否有人可以提供以下帮助:
我有一个这样的 xml 文档:
<div class="_cl">element description 1</div>
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
<div class="_cl">element description 2</div>
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
我想知道 linq 中是否有任何方法可以按没有子元素的元素对其进行分组。基本上,尝试像这样构造文档:
<div class="_cl">element description 1
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
</div>
<div class="_cl">element description 2
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
<div class="_cl">
<anotherele>dtls</anotherele>
<anotherele>moredtls</anotherele>
</div>
</div>
我的第一次可悲的尝试是这样的:
var n = from a in doc.Descendants()
where a.Name.LocalName == "div" && (string)a.Attribute("class") == "_cl"
group a by a.Value.Length<50 into g
select new { k = g.Key, p = g.Count() };
希望这是有道理的,并提前感谢。
学习
【问题讨论】:
-
我真的不明白你想做什么......你不能按条件分组 - 你可以按字段分组......没有子元素是一个条件,你可以过滤由它,但不是组。
-
如果你说的是真的,那就不可能了。您还有其他解决方案来塑造 xml 吗?
-
我的意思是,您的请求不合逻辑...请更正确地解释您要做什么。示例输出可能会很好。
标签: c# .net linq linq-to-xml