Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
XML
- <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:

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

                                   };
Posted
Updated 27-Feb-18 1:36am
v2

1 solution

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:
C#
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...
    }
}
 
Share this answer
 
Comments
Richard Deeming 27-Feb-18 11:29am    
There's no need for .Descendants().SelectMany(x => x.Elements("name")) - you can simply use .Descendants("name") instead.

Also, if you replace itemDimensions.Elements("x") with itemDimensions.Element("x"), then x.Attributes().Where(x => x.Name == "name").Select(x => x.Value).FirstOrDefault() can be replaced with (string)x.Attribute("name").

But you'll need a null check on the dimension elements, since some products don't include a height or length.
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 27-Feb-18 11:50am    
Thanks Richard... the code was thrown together quickly without optimisation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900