LINQ to XML:创建复杂的匿名类型
我有一个 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 节点可能包含许多 Product 节点。为此,我想构建一个具有以下属性的匿名对象的列表:
name
section
image
thumb
我能够使用 XDocument 获取 Product 元素的列表。
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
I've an xml file as follows:
<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>
In production the ProductGroup node might contain many Product nodes. For this I kind of want to build a list of an anonymous object that has the following properties:
name
section
image
thumb
I am able to get a list of Product elements using XDocument.
Dim doc As XDocument = XDocument.Load("ProductsGroups.xml")
Dim lstProducts = from x In doc Where CType(c.Element("price"), Integer) < 54
From here what do I do?
Update:
Let me explain this better. I am not sure if I have communicated this properly.
Taking the above xml example itself. The above code I've written returns all product elements with the specified "where" condition. Now for each XmlElement returned (product) I've to create n-number of anonymous objects. The number n depends on how many children are there for the PopupImages and ImageThumbs nodes. In my case however, the number will be the same. Hence coming back to the above example, I'd get two anonymous objects:
Anonymous1 Anonymous2
---------- ----------
name Roses Roses
section Floral Floral
image img1.jpg img2.jpg
thumb img1-thm.jpg img2-thm.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这种方法:
如果您确实需要一个列表,只需调用
query.ToList()
并将结果存储在变量中,或者将原始查询括在括号中并附加ToList()
(为了可读性,我不想这样做)。同样,图像和缩略图当前的类型为IEnumerable
,因此如果您需要列表或数组,请添加适当的扩展方法调用。Try this approach:
If you really need a list just call
query.ToList()
and store the result in a variable or enclose the original query in parentheses and appendToList()
(I prefer not to do this for readability). Similarly, the images and thumbnails are currently of typeIEnumerable<string>
, so if you need a list or array add the appropriate extension method call.我不熟悉 VB.Net,但在 C# 中你会写这样的内容:
希望这有帮助。
编辑:应该处理合并 PopupImages 和 ImageThumbs 的新查询:
仍在 C# 中,但我认为这个想法很明确。
I'm not familiar with VB.Net, but in C# you'd write something like this:
Hope this helps.
EDIT: new query that should handle merging PopupImages and ImageThumbs:
Still in C#, but I think the idea is clear.