简体   繁体   中英

Get variation from Amazon MWS XML product feed

I'm using the Amazon MWS C# Client library and I can successfully return an XML feed but I cannot seem to parse the Relationships elements whatsoever. This is the official Product API documentation

Here is the XML returned to me:

<Product>
   <Identifiers>
     <MarketplaceASIN>
       <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
       <ASIN>B0769TXBK4</ASIN>
     </MarketplaceASIN>
   </Identifiers>
   <AttributeSets>
     <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-GB">
       <ns2:Brand>Ambi Pur</ns2:Brand>
       <ns2:Label>Ambi Pur</ns2:Label>
       <ns2:Manufacturer>Ambi Pur</ns2:Manufacturer>
       <ns2:NumberOfItems>1</ns2:NumberOfItems>
       <ns2:PackageDimensions>
         <ns2:Height Units="inches">2.5590551155</ns2:Height>
         <ns2:Length Units="inches">6.6929133790</ns2:Length>
         <ns2:Width Units="inches">4.5275590505</ns2:Width>
         <ns2:Weight Units="pounds">0.2645547144</ns2:Weight>
       </ns2:PackageDimensions>
       <ns2:PackageQuantity>2</ns2:PackageQuantity>
       <ns2:PartNumber>95535</ns2:PartNumber>
       <ns2:ProductGroup>BISS Basic</ns2:ProductGroup>
       <ns2:ProductTypeName>ADHESIVES_AND_SEALANTS</ns2:ProductTypeName>
       <ns2:Publisher>Ambi Pur</ns2:Publisher>
       <ns2:SmallImage>
         <ns2:URL>http://ecx.images-amazon.com/images/I/41DhkI6B8oL._SL75_.jpg</ns2:URL>
         <ns2:Height Units="pixels">75</ns2:Height>
         <ns2:Width Units="pixels">75</ns2:Width>
       </ns2:SmallImage>
       <ns2:Studio>Ambi Pur</ns2:Studio>
       <ns2:Title>Ambi Pur 3Volution Air Freshener - 1 Plug-In Diffuser (2 Packs)</ns2:Title>
     </ns2:ItemAttributes>
   </AttributeSets>
   <Relationships>
     <VariationParent xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
      <Identifiers>
        <MarketplaceASIN>
          <MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
          <ASIN>B076B1GP37</ASIN>
        </MarketplaceASIN>
      </Identifiers>
    </VariationParent>
  </Relationships>
  <SalesRankings>
    <SalesRank>
      <ProductCategoryId>biss_basic_display_on_website</ProductCategoryId>
      <Rank>1866</Rank>
    </SalesRank>
    <SalesRank>
      <ProductCategoryId>301308031</ProductCategoryId>
      <Rank>832</Rank>
    </SalesRank>
  </SalesRankings>
</Product>

Here is the code, the AttributeSets foreach works fine but I cannot seem to traverse the Relationships elements without getting 'Object reference not set to an instance of an object'.

ListMatchingProductsResponse lmpr = (ListMatchingProductsResponse)response;

foreach (var x in lmpr.ListMatchingProductsResult.Products.Product)
{
    Console.WriteLine("ASIN: " + x.Identifiers.MarketplaceASIN.ASIN);

    int i = 0;
    SalesRankList rankings = x.SalesRankings;
    List<SalesRankType> salesRankList = rankings.SalesRank;

    foreach (SalesRankType saleRankings in salesRankList)
    {
        for (; i < 1; i++)
        {
            if (saleRankings.IsSetRank())
            {
                Console.WriteLine("Sales Rank: " + Convert.ToString(saleRankings.Rank));
            }
        }

    }

    foreach (var relationship in x.Relationships.Any)
    {

        string xml = ProductsUtil.FormatXml((System.Xml.XmlElement)relationship);
        XElement element = XElement.Parse(xml);

        XNamespace ns2 = "{http://mws.amazonservices.com/schema/Products/2011-10-01}";

        // Errors here!
        Console.WriteLine(element.Element(ns2 + "VariationParent").Element(ns2 + "Identifiers")); 


        }

        foreach (var attribute in x.AttributeSets.Any)
        {
            string xml = ProductsUtil.FormatXml((System.Xml.XmlElement)attribute);
            XElement element = XElement.Parse(xml);
            XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd";

            Console.WriteLine("Title: " + element.Element(ns2 + "Title").Value);
            Console.WriteLine("Title: " + element.Element(ns2 + "Title").Name);
            Console.WriteLine("Pack Quantity: " + element.Element(ns2 + "PackageQuantity").Value);

            if ((string)element.Element(ns2 + "ListPrice") != null)
            {
                Console.WriteLine("List Price:" + element.Element(ns2 + "ListPrice").Element(ns2 + "Amount").Value);
            }
        }

        lstASIN.ASIN.Add(x.Identifiers.MarketplaceASIN.ASIN);
        Console.WriteLine("");
    }
}

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement relationships = doc.Descendants("Relationships").FirstOrDefault();

            XNamespace ns = "http://mws.amazonservices.com/schema/Products/2011-10-01";
            string marketplaceId = (string)relationships.Descendants(ns + "MarketplaceId").FirstOrDefault();
        }
    }
}

This seems to work but still feels like unnecessary traversion.

RelationshipList rlist = x.Relationships;

if (rlist.IsSetAny())
 {

   foreach (var relationship in rlist.Any)
     {

     string xml = ProductsUtil.FormatXml((System.Xml.XmlElement)relationship);

      XDocument xDoc = XDocument.Parse(xml);
      XNamespace ns2 = "http://mws.amazonservices.com/schema/Products/2011-10-01";

      IEnumerable<object> relationships = xDoc.Descendants();

      foreach (System.Xml.Linq.XElement xe in relationships)
        {

          if (xe.Name == ns2 + "ASIN")
                        {

                            Console.WriteLine(xe.Value);

                        }                     

         }

      }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM