tags:

views:

1803

answers:

2

Hi all, I need return a list of the element <AssetText>. My query below only returns back the first AssetText. Any thoughts much appreciated.

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");

 

<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>
+3  A: 

Change Element("AssetText") to Elements("AssetText") to get all the AssetText elements. Note that you'll need to change the rest of your query as well, otherwise it'll only match when the first ProductView has a ViewId of 44. I suggest you use a second "from" clause:

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");
Jon Skeet
A: 

hey thanks very much appreciated. Very useful.

frosty