linq到xml:如何从元素中选择值
我需要返回元素的列表linq到xml:如何从元素中选择值,xml,linq,Xml,Linq,我需要返回元素的列表。下面的查询只返回第一个AssetText。非常感谢您的任何想法 var q = from c in xDoc.Descendants("Product") where (int) c.Element("ProductView").Element("ViewId") == 44 select (string) c.Element("ProductView").Element("AssetText").Element("Text"); 44
。下面的查询只返回第一个AssetText
。非常感谢您的任何想法
var q = from c in xDoc.Descendants("Product")
where (int) c.Element("ProductView").Element("ViewId") == 44
select (string) c.Element("ProductView").Element("AssetText").Element("Text");
44
我的第一个资产文本
我的第二个资产文本
45
我的第三个资产文本
将元素(“AssetText”)
更改为元素(“AssetText”)
,以获取所有AssetText元素。请注意,您还需要更改查询的其余部分,否则它将仅在第一个ProductView的ViewId为44时匹配。我建议您使用第二个“from”条款:
<Product>
<ProductView>
<ViewId>44</ViewId>
<AssetText>
<Text>my first Asset Text</Text>
</AssetText>
<AssetText>
<Text>my second Asset Text</Text>
</AssetText>
</ProductView>
<ProductView>
<ViewId>45</ViewId>
<AssetText>
<Text>my third Asset Text</Text>
</AssetText>
</ProductView>
</Product>
var q = from c in xDoc.Descendants("Product")
from view in c.Elements("ProductView")
where (int) view.Element("ViewId") == 44
from assetText in view.Elements("AssetText")
select assetText.Element("Text");