我有一個xml文件,如下所示:的LINQ to XML:創建複雜的匿名類型
<ProductGroup>
<Product id="4601A">
<name>Roses</name>
<section>Floral</section>
<price>46</price>
<PopupImages>
<PopupImage>img1.jpg</PopupImage>
<PopupImage>img2.jpg</PopupImage>
</PopupImages>
<ImageThumbs>
<thumb>img1-thm.jpg</thumb>
<thumb>img2-thm.jpg</thumb>
</ImageThumbs>
</Product>
</ProductGroup>
在生產ProductGroup節點可能包含許多產品的節點。爲此,我有種想建立一個具有以下屬性的匿名對象的列表:
name
section
image
thumb
我能夠得到使用的XDocument產品元素的列表。
Dim doc As XDocument = XDocument.Load("ProductsGroups.xml")
Dim lstProducts = from x In doc Where CType(c.Element("price"), Integer) < 54
從這裏我該怎麼辦?
更新:
讓我更好地解釋這一點。我不確定我是否正確傳達了這一點。
以上面的xml示例本身。我寫的上面的代碼返回具有指定「where」條件的所有產品元素。現在每個返回的XmlElement(產品)我必須創建n個匿名對象。數字n取決於PopupImages和ImageThumbs節點有多少個孩子。然而,就我而言,這個數字將是一樣的。因此回來上面的例子中,我得到了兩個匿名對象:
Anonymous1 Anonymous2
---------- ----------
name Roses Roses
section Floral Floral
image img1.jpg img2.jpg
thumb img1-thm.jpg img2-thm.jpg
檢查更新 – deostroll 2011-06-11 10:07:00
@deostroll檢查更新:) – Ladislav 2011-06-13 21:19:43