LINQ to XML:创建复杂的匿名类型 [英] LINQ to XML: creating complex anonymous type

查看:205
本文介绍了LINQ to XML:创建复杂的匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,如下所示:

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>

在生产中,ProductGroup节点可能包含许多Product节点.为此,我想构建一个具有以下属性的匿名对象的列表:

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

我能够使用XDocument获得Product元素的列表.

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

从这里我该怎么办?

更新:

让我更好地解释一下.我不确定我是否已正确传达此信息.

Let me explain this better. I am not sure if I have communicated this properly.

以上述xml示例本身为例.我编写的上述代码返回具有指定"where"条件的所有产品元素.现在对于每个返回的XmlElement(产品),我必须创建n个匿名对象.数字n取决于PopupImages和ImageThumbs节点的子级数.但是,在我的情况下,该数字将相同.因此,回到上面的示例,我将获得两个匿名对象:

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

推荐答案

尝试以下方法:

Dim query = From product In doc.Elements("Product") 
            Where Integer.Parse(product.Element("price").Value) < 54 
            Select New With
            {
                .Name = product.Element("name").Value,
                .Section = product.Element("section").Value, 
                .Images = product.Descendants("PopupImage").Select(Function(i) i.Value), 
                .Thumbs = product.Descendants("thumb").Select(Function(t) t.Value) 
            }

For Each item in query
    Console.WriteLine(item.Name)
    Console.WriteLine(item.Section)
    Console.WriteLine("Images:")
    For Each image in item.Images
        Console.WriteLine("  " + image)
    Next
    Console.WriteLine("Thumbs:")
    For Each thumb in item.Thumbs
        Console.WriteLine("  " + thumb)
    Next
Next

如果您真的需要一个列表,只需调用query.ToList()并将结果存储在变量中或将原始查询括在括号中并附加ToList()(出于可读性考虑,我不愿这样做).同样,图像和缩略图当前的类型为IEnumerable<string>,因此,如果需要列表或数组,请添加适当的扩展方法调用.

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 append ToList() (I prefer not to do this for readability). Similarly, the images and thumbnails are currently of type IEnumerable<string>, so if you need a list or array add the appropriate extension method call.

这篇关于LINQ to XML:创建复杂的匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆