读书人

怎么在LINQ FOR XML只返回似主

发布时间: 2012-12-18 12:43:41 作者: rapoo

如何在LINQ FOR XML只返回似主表的信息?
本帖最后由 gxpotato 于 2012-03-01 14:02:15 编辑 假有XML
<root>
<man>
<home>
<children1 group_name="brother">
<child name="tom1" />
<child name="jack1" />
</children1>
<children2 group_name="sister">
<child name="tom2" />
<child name="jack2" />

</children2>
</home>
</man>
</root>


我怎用LINQ FOR XML查child 的 attribute name包含 “tom”,然後返回一似的料?似主表的
<home>
<children1 group_name="brother">
<child name="tom" />
</children>
<children2 group_name="sister">
<child name="tom2" />
</children2>

</home>

我只要返回children1,就把所有child都,太了,搞了半天搞定。。。我只想要一child,而不是所有child。
[最优解释]
找到 不包含tom 的节点,并且删除它们,剩余就是你要的了
类似:
XElement xe=XElement.Parse();
xe.Descendants("child ").Where(w=>!w.Attribute("name").value.Contains("tom")).Remove();
// xe 就差不多是你想要的吧
[其他解释]
我先,我的率是很高的。我希望付出的人都有回。
[其他解释]
怎把字符型的在lamba表式面整型判呢?

xe.Descendants("child ").Where(w=>!w.Attribute("name").value.Contains("tom")).Remove();

如果我的xml面的name是字,但是用字符保存,在lamba面怎再int?了一下,是搞不定。

读书人网 >.NET

热点推荐