LINQ XML子查询-需要帮助 [英] LINQ XML Subquery - Need Assistance

查看:50
本文介绍了LINQ XML子查询-需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我当前的LINQ查询和示例XML.我想做的是从中选择主要图片网址以及其他属性,例如修剪,主体,品牌,型号等.

My current LINQ query and example XML are below. What I'd like to do is select the primary image urls from the as well as the other attributes such as trim, body, make, model, etc.

做到这一点的最佳方法是什么?

What is the best way to do this?

<response>
<objects>
    <object>
        <trim>LT</trim>
        <body>4dr Car</body>
        <warranty_miles />
        <color>WHITE</color>
        <selling_price>0.00</selling_price>
        <vin>1g1jc5sh3c4198042</vin>
        <has_service_contract >False</has_service_contract>
        <year>2012</year>
        <alt_id></alt_id>
        <stock_num>1205362</stock_num>
        <has_warranty>False</has_warranty>
        <warranty_month />
        <make>CHEVROLET</make>
        <date_manufactured />
        <interior_color>Dark Pewter/Dark Titanium</interior_color>
        <date_updated>2012-10-17</date_updated>
        <date_dealer_created>2012-06-06</date_dealer_created>
        <certification_warranty />
        <status/>
        <vevo></vevo>
        <image_urls>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/1764e186eabe4ae892f6230107b862cf.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/3eeb7050770849e8937191d09453d936.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/7f6c1f172f7b4944ac2d165c0dfbcb09.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/82d242ba07bb4bb9a7c8514ea1aadae7.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/93e3cac89d0f4872b86acd9c2d94695a.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/edec4ffe9b2349ffabea9dcb25a78d1e.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/f2c1dbb15fec4cf6a62b7842db22763a.jpg</url>
                <resource_uri></resource_uri>
            </object>
            <object>
                <url>http://content.homenetiol.com/1593/69025/640x480/fa7341e250654533a9de2a756d4274eb.jpg</url>
                <resource_uri></resource_uri>
            </object>
          </image_urls>
       </object>
   </objects>
 </response>

这是我到目前为止所拥有的

Here is what I have so far

        var users = from m in main2.Elements("object")
                    select new Inventory 
                    {
                        Make = (string)m.Element("make"),
                        Year = (string)m.Element("year"),
                        Model = (string)m.Element("model"),
                    };

推荐答案

您想要将image_urls节点下的对象集合投影到列表中.

You want to project the collection of objects under the image_urls node into a list.

这会将其投影到字符串列表中.

This will project it into a list of strings.

select new Inventory
{
    Make = (string)m.Element("make"),
    Year = (string)m.Element("year"),
    Model = (string)m.Element("model"),
    Urls = m.Element("image_urls").Elements().Select(e => (string)e.Element("url"))
};

这篇关于LINQ XML子查询-需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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