我是LINQ的新手,並且遇到問題。我有一個看起來是這樣的文件:使用LINQ來選擇一個隨機XML節點
<?xml version="1.0" encoding="utf-8" ?>
<Galleries>
<Gallery ID="10C31804CEDB42693AADD760C854ABD" Title="Test1">
<Description>The first test gallery. Picture of a cat and Wilford Brimley. Can you tell the difference?</Description>
<Images>
<Image Title="t1Image1" FileName="tcats.jpg" />
<Image Title="t1Image2" FileName="twb.jpg" />
</Images>
</Gallery>
<Gallery ID="0420EC15405B488E1E0F157AC823A6" Title="Test2">
<Description>The second test gallery. A large image of Wilford Brimley and various cats. The cats will be on the right.</Description>
<Images>
<Image Title="t2Image1" FileName="wilfordbrimley.jpg" />
</Images>
</Gallery>
</Galleries>
不管怎樣,我知道我要的畫廊的ID,但我想隨機選擇的圖像之一。有沒有可以做到這一點的LINQ聲明?
一個非常聰明的解決方案。 – 2010-01-14 00:23:57
我想出瞭如何使用Random.Next()來選擇一個隨機圖庫,但是如何在給定圖庫ID的圖庫下選擇一個隨機圖像? 對不起,LINQ現在正在審查我的頭。 – 2010-01-14 00:29:22
@OSMan:如果你有一個包含Gallery的'XElement'或'XNode',只需使用'node.Element(「Images」)'獲取圖像元素。 – 2010-01-14 00:34:35