Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
linq到xml:如何从元素中选择值_Xml_Linq - Fatal编程技术网

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