如何从这个XML文件解析itemdimension并获取每个asin的高度,重量,长度,宽度值? [英] How can parse the itemdimension from this XML file and get values of height, weight, length, width for each asin ?

查看:109
本文介绍了如何从这个XML文件解析itemdimension并获取每个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>





What I have tried:





What I have tried:

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),

                                   };

推荐答案

Firstly, you have an error in the XML data sample on line# 443.



I would map classes to the XML data and convert. But it you want to manually read the xml data, then you could do something like this:

Firstly, you have an error in the XML data sample on line# 443.

I would map classes to the XML data and convert. But it you want to manually read the xml data, then you could do something like this:
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...
    }
}


这篇关于如何从这个XML文件解析itemdimension并获取每个asin的高度,重量,长度,宽度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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