获得一组使用LINQ元素 [英] Getting a set of elements using linq
本文介绍了获得一组使用LINQ元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的XML片段:
< XML>
< ObsCont xCampo =字段1>
< xTexto>例1< / xTexto>
< / ObsCont>
< ObsCont xCampo =场2>
< xTexto>例2< / xTexto>
< / ObsCont>
< ObsCont xCampo =FIELD3>
< xTexto>例3< / xTexto>
< / ObsCont>
< / XML>
我如何(使用LINQ)获得,例如什么是具有作为父xTexto标签内的xCampo属性场2的ObsCont?
(C#,vb.net,你的选择)
解决方案
的XDocument XML = XDocument.Parse(@<您的XML>中);
从在xml.Elements域(ObsCont)
其中,field.Attribute(xCampo)= NULL和放大器;!&安培;
field.Attribute(xCampo)。价值==场2
选择field.Element(xTexto)的价值。
这会返回一个IEnumerable包含所有的值与您指定的标准类型的字符串。
I have the following piece of XML:
<xml>
<ObsCont xCampo="field1">
<xTexto>example1</xTexto>
</ObsCont>
<ObsCont xCampo="field2">
<xTexto>example2</xTexto>
</ObsCont>
<ObsCont xCampo="field3">
<xTexto>example3</xTexto>
</ObsCont>
</xml>
How do I (using linq) get for example what is inside the xTexto tag that has as parent the ObsCont with the xCampo attribute "field2" ?
(c#, vb.net, your choice)
解决方案
XDocument xml = XDocument.Parse(@"<your XML>");
from field in xml.Elements("ObsCont")
where field.Attribute("xCampo") != null &&
field.Attribute("xCampo").Value == "field2"
select field.Element("xTexto").Value;
This returns an IEnumerable of type string containing all of the values with the criteria that you specified.
这篇关于获得一组使用LINQ元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文