Member 12646622 Ответов: 1

Как можно проанализировать itemdimension из этого XML-файла и получить значения высоты, веса, длины, ширины для каждого asin ?


- <GetMatchingProductResponse>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B000HDOO02</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>CableWholesale</Brand> 
  <Color>black</Color> 
- <ItemDimensions>
  <Height Units="inches">1.18</Height> 
  <Length Units="inches">1.18</Length> 
  <Width Units="inches">1.18</Width> 
  <Weight Units="pounds">0.55</Weight> 
  </ItemDimensions>
  <Label>CableWholesale</Label> 
  <Manufacturer>CableWholesale</Manufacturer> 
  <Model>10CO-02206</Model> 
- <PackageDimensions>
  <Height Units="inches">0.39</Height> 
  <Length Units="inches">0.39</Length> 
  <Width Units="inches">0.39</Width> 
  <Weight Units="pounds">0.55</Weight> 
  </PackageDimensions>
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>10CO-02206</PartNumber> 
  <ProductGroup>CE</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>CableWholesale</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/21e--bAGECL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>CableWholesale</Studio> 
  <Title>CableWholesale's Cisco Compatible Serial Cable, HD60 Male to DB25 Female, Equivalent to CAB-232FC-6, 6 foot</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
- <SalesRankings>
- <SalesRank>
  <ProductCategoryId>464404</ProductCategoryId> 
  <Rank>2812</Rank> 
  </SalesRank>
- <SalesRank>
  <ProductCategoryId>12954861</ProductCategoryId> 
  <Rank>331167</Rank> 
  </SalesRank>
  </SalesRankings>
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B000HDJXSA</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>CableWholesale</Brand> 
  <Color>black</Color> 
- <ItemDimensions>
  <Height Units="inches">1.18</Height> 
  <Length Units="inches">1.18</Length> 
  <Width Units="inches">1.18</Width> 
  <Weight Units="pounds">0.85</Weight> 
  </ItemDimensions>
  <Label>CableWholesale</Label> 
- <ListPrice>
  <Amount>33.99</Amount> 
  <CurrencyCode>USD</CurrencyCode> 
  </ListPrice>
  <Manufacturer>CableWholesale</Manufacturer> 
  <Model>10CO-03210</Model> 
- <PackageDimensions>
  <Height Units="inches">0.39</Height> 
  <Length Units="inches">0.39</Length> 
  <Width Units="inches">0.39</Width> 
  <Weight Units="pounds">0.85</Weight> 
  </PackageDimensions>
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>10CO-03210</PartNumber> 
  <ProductGroup>CE</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>CableWholesale</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/416xm3zp5AL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>CableWholesale</Studio> 
  <Title>CableWholesale's Cisco Compatible Serial Cable, HD60 Male to DB37 Female, Equivalent to CAB-449FC-3M, 10 foot</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
- <SalesRankings>
- <SalesRank>
  <ProductCategoryId>172472</ProductCategoryId> 
  <Rank>31788</Rank> 
  </SalesRank>
  </SalesRankings>
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B00ANARER4</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>Monoprice</Brand> 
- <ItemDimensions>
  <Weight Units="pounds">6.97</Weight> 
  </ItemDimensions>
  <Label>MonoPrice</Label> 
  <Manufacturer>MonoPrice</Manufacturer> 
  <Model>342</Model> 
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>342</PartNumber> 
  <ProductGroup>PC Accessory</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>MonoPrice</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/21XiTO%2BwU0L._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>MonoPrice</Studio> 
  <Title>10FT HD60M/DB37F Cable (CAB-449FC-3M) [Electronics]</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
  <SalesRankings /> 
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B000I94H9I</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>CableWholesale</Brand> 
  <Color>black</Color> 
- <ItemDimensions>
  <Height Units="inches">1.18</Height> 
  <Length Units="inches">1.18</Length> 
  <Width Units="inches">1.18</Width> 
  <Weight Units="pounds">0.65</Weight> 
  </ItemDimensions>
  <Label>CableWholesale</Label> 
- <ListPrice>
  <Amount>31.99</Amount> 
  <CurrencyCode>USD</CurrencyCode> 
  </ListPrice>
  <Manufacturer>CableWholesale</Manufacturer> 
  <Model>10CO-03106</Model> 
- <PackageDimensions>
  <Height Units="inches">0.39</Height> 
  <Length Units="inches">0.39</Length> 
  <Width Units="inches">0.39</Width> 
  <Weight Units="pounds">0.65</Weight> 
  </PackageDimensions>
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>10CO-03106</PartNumber> 
  <ProductGroup>CE</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>CableWholesale</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/21KXsE%2BsRiL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>CableWholesale</Studio> 
  <Title>CableWholesale's Cisco Compatible Serial Cable, HD60 Male to DB37 Male, Equivalent to CAB-449MT-6, 6 foot</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
  <SalesRankings /> 
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B000HDKZUK</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>Cblwhl</Brand> 
- <ItemDimensions>
  <Height Units="inches">1.18</Height> 
  <Length Units="inches">1.18</Length> 
  <Width Units="inches">1.18</Width> 
  <Weight Units="pounds">1.00</Weight> 
  </ItemDimensions>
  <Label>CblWhl</Label> 
  <Manufacturer>CblWhl</Manufacturer> 
  <Model>10CO-04110</Model> 
- <PackageDimensions>
  <Height Units="inches">0.39</Height> 
  <Length Units="inches">0.39</Length> 
  <Width Units="inches">0.39</Width> 
  <Weight Units="pounds">2.00</Weight> 
  </PackageDimensions>
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>10CO-04110</PartNumber> 
  <ProductGroup>PC Accessory</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>CblWhl</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/41wZQpwFDnL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>CblWhl</Studio> 
  <Title>HD60 Male / v.35 Male, (CAB-V35MT-3M) Cisco Cable, 10 ft</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
- <SalesRankings>
- <SalesRank>
  <ProductCategoryId>172472</ProductCategoryId> 
  <Rank>32342</Rank> 
  </SalesRank>
- <SalesRank>
  <ProductCategoryId>172532</ProductCategoryId> 
  <Rank>501906</Rank> 
  </SalesRank>
  </SalesRankings>
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B00ANARFGY</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>Monoprice</Brand> 
- <ItemDimensions>
  <Weight Units="pounds">7.50</Weight> 
  </ItemDimensions>
  <Label>MonoPrice</Label> 
  <Manufacturer>MonoPrice</Manufacturer> 
  <Model>343</Model> 
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>343</PartNumber> 
  <ProductGroup>PC Accessory</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>MonoPrice</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/31JW99bY-tL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>MonoPrice</Studio> 
  <Title>10FT HD60M/V.35M Cable (CAB-V35MT-3M) [Electronics]</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
  <SalesRankings /> 
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B0044AIFV8</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>QVS</Brand> 
  <Label>QVS</Label> 
  <Manufacturer>QVS</Manufacturer> 
  <Model>CABV35MT-06</Model> 
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>CABV35MT-06</PartNumber> 
  <ProductGroup>PC Accessory</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>QVS</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/41nhvb9KDaL._SL75_.jpg</URL> 
  <Height Units="pixels">29</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>QVS</Studio> 
  <Title>QVS 6ft DB60 to DTE V.35 Serial Cisco Router Cable</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
- <SalesRankings>
- <SalesRank>
  <ProductCategoryId>464404</ProductCategoryId> 
  <Rank>3408</Rank> 
  </SalesRank>
- <SalesRank>
  <ProductCategoryId>12954861</ProductCategoryId> 
  <Rank>393799</Rank> 
  </SalesRank>
  </SalesRankings>
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B00ANARDQ6</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>Monoprice</Brand> 
- <ItemDimensions>
  <Weight Units="pounds">4.50</Weight> 
  </ItemDimensions>
  <Label>MonoPrice</Label> 
  <Manufacturer>MonoPrice</Manufacturer> 
  <Model>338</Model> 
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>338</PartNumber> 
  <ProductGroup>CE</ProductGroup> 
  <ProductTypeName>CONSUMER_ELECTRONICS</ProductTypeName> 
  <Publisher>MonoPrice</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/21%2BcEvLU8zL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>MonoPrice</Studio> 
  <Title>10FT HD60M/DB15F Cable (CAB-X21FC-3M) [Electronics]</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
  <SalesRankings /> 
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B000HDJXRQ</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>Cblwhl</Brand> 
  <Color>black</Color> 
- <ItemDimensions>
  <Height Units="inches">1.18</Height> 
  <Length Units="inches">1.18</Length> 
  <Width Units="inches">1.18</Width> 
  <Weight Units="pounds">0.78</Weight> 
  </ItemDimensions>
  <Label>CblWhl</Label> 
  <Manufacturer>CblWhl</Manufacturer> 
  <Model>10CO-02210</Model> 
- <PackageDimensions>
  <Height Units="inches">0.39</Height> 
  <Length Units="inches">0.39</Length> 
  <Width Units="inches">0.39</Width> 
  <Weight Units="pounds">1.78</Weight> 
  </PackageDimensions>
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>10CO-02210</PartNumber> 
  <ProductGroup>CE</ProductGroup> 
  <ProductTypeName>COMPUTER_ADD_ON</ProductTypeName> 
  <Publisher>CblWhl</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/21e--bAGECL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>CblWhl</Studio> 
  <Title>CableWholesale's Cisco Compatible Serial Cable, HD60 Male to DB25 Female, Equivalent to CAB-232FC-3M, 10 foot</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
- <SalesRankings>
- <SalesRank>
  <ProductCategoryId>172472</ProductCategoryId> 
  <Rank>31006</Rank> 
  </SalesRank>
  </SalesRankings>
  </Product>
  </GetMatchingProductResult>
- <GetMatchingProductResult status="Success">
- <Product>
- <Identifiers>
- <MarketplaceASIN>
  <MarketplaceId>ATVPDKIKX0DER</MarketplaceId> 
  <ASIN>B017WQHU70</ASIN> 
  </MarketplaceASIN>
  </Identifiers>
- <AttributeSets>
- <ItemAttributes lang="en-US">
  <Binding>Electronics</Binding> 
  <Brand>Cables Unlimited</Brand> 
  <Label>Audio Unlimited®</Label> 
  <Manufacturer>Audio Unlimited®</Manufacturer> 
  <Model>SPK-TRANS2</Model> 
  <PackageQuantity>1</PackageQuantity> 
  <PartNumber>SPK-TRANS2</PartNumber> 
  <ProductGroup>CE</ProductGroup> 
  <ProductTypeName>PORTABLE_AUDIO</ProductTypeName> 
  <Publisher>Audio Unlimited®</Publisher> 
- <SmallImage>
  <URL>http://ecx.images-amazon.com/images/I/31ew-AMm9ZL._SL75_.jpg</URL> 
  <Height Units="pixels">75</Height> 
  <Width Units="pixels">75</Width> 
  </SmallImage>
  <Studio>Audio Unlimited®</Studio> 
  <Title>Audio Unlimited SPK-TRANS2 Dual Powered Transmitter for SPK-ROCK & SPK-VELO series (White)</Title> 
  </ItemAttributes>
  </AttributeSets>
  <Relationships /> 
- <SalesRankings>
- <SalesRank>
  <ProductCategoryId>ce_display_on_website</ProductCategoryId> 
  <Rank>568606</Rank> 
  </SalesRank>
  </SalesRankings>
  </Product>
  </GetMatchingProductResult>
  </GetMatchingProductResponse>


Что я уже пробовал:

main.Element("ResponseMetadata").Remove();
                    foreach (XElement XE in main.DescendantsAndSelf())
                    { 
                        XE.Name = XE.Name.LocalName; 
                        XE.ReplaceAttributes(
                                                (from xattrib in XE.Attributes().Where(xa => !xa.IsNamespaceDeclaration)
                                                        select new XAttribute(
                                                         xattrib.Name.LocalName,
                                                         xattrib.Value
                                                  )
                                             )
                                 );
                    } 
                    
                    var products =  from m in main.Descendants("GetMatchingProductResult").Elements("Product")
                                   let sr = m.Descendants("AttributeSets").Elements("ItemAttributes").FirstOrDefault()
                                   select new
                                   {
                                       ASIN = (m == null || m.Value.Length == 0 ? "" : m.Descendants("Identifiers").Elements("MarketplaceASIN").Elements("ASIN").FirstOrDefault().Value),
                                       Height = (string)(sr == null || sr.Value.Length == 0 ? "" : sr.Descendants("ItemDimensions").Elements("Height").FirstOrDefault().Value),
                                       Length = (m == null || m.Value.Length == 0 ? "" : sr.Descendants("ItemDimensions").Elements("Length").FirstOrDefault().Value),
                                       Width = (m == null || m.Value.Length == 0 ? "" : sr.Descendants("ItemDimensions").Elements("Width").FirstOrDefault().Value),
                                       Weight = (m == null || m.Value.Length == 0 ? "" : sr.Descendants("ItemDimensions").Elements("Weight").FirstOrDefault().Value),

                                   };

1 Ответов

Рейтинг:
1

Graeme_Grant

Во-первых, у вас есть ошибка в образце XML-данных в строке# 443.

Я бы сопоставил классы с XML - данными и преобразовал их. Но если вы хотите вручную прочитать xml-данные, то вы можете сделать что-то вроде этого:

var doc = XDocument.Parse(rawXml);

var products = doc.Descendants()
                  .SelectMany(x => x.Elements("Product"));

foreach (var product in products)
{
    var itemDimensions = product.Descendants()
                                .SelectMany(x => x.Elements("ItemDimensions"))
                                .FirstOrDefault();
    if (itemDimensions != null)
    {
        var heightElement = itemDimensions.Elements("Height");
        var heightUnits = heightElement.Attributes()
                                       .Where(x => x.Name == "Units")
                                       .Select(x => x.Value)
                                       .FirstOrDefault();
        var heightVale = heightElement.First().Value;

        var lengthElement = itemDimensions.Elements("Length");
        var lengthUnits = lengthElement.Attributes()
                                       .Where(x => x.Name == "Units")
                                       .Select(x => x.Value)
                                       .FirstOrDefault();
        var lengthValue = lengthElement.First().Value;

        // other elements to be implemented here...
    }
}


Richard Deeming

В этом нет необходимости. .Descendants().SelectMany(x => x.Elements("name")) - вы можете просто использовать .Descendants("name") вместо.

Кроме того, если вы замените itemDimensions.Elements("x") с itemDimensions.Element("x"), затем x.Attributes().Where(x => x.Name == "name").Select(x => x.Value).FirstOrDefault() может быть заменен на (string)x.Attribute("name").

Но вам понадобится нулевая проверка элементов измерения, так как некоторые продукты не включают высоту или длину.

var products = doc.Descendants("Product");
foreach (var product in products)
{
    var itemDimensions = product.Descendants("ItemDimensions").FirstOrDefault();
    if (itemDimensions != null)
    {
        var heightElement = itemDimensions.Element("Height");
        var heightUnits = (string)heightElement?.Attribute("Units");
        var heightValue = (string)heightElement;

        var lengthElement = itemDimensions.Element("Length");
        var lengthUnits = (string)lengthElement?.Attribute("Units");
        var lengthValue = (string)lengthElement;

        // other elements to be implemented here...
    }
}

Graeme_Grant

Спасибо, Ричард... код был скомпонован быстро, без оптимизации.