从亚马逊MWS XML产品Feed获取变体

时间:2018-05-25 15:25:35

标签: c# xml amazon-mws

我正在使用亚马逊MWS C#客户端库,我可以成功返回XML提要但我似乎无法解析关系元素。 This is the official Product API documentation

以下是返回给我的XML:

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

这是代码,AttributeSets foreach工作正常但我似乎无法遍历Rel​​ationships元素而不会将'Object reference未设置为对象的实例'。

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("");
    }
}

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

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();
        }
    }
}

答案 1 :(得分:0)

这似乎有效但仍然感觉不必要的颠覆。

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

                        }                     

         }

      }

}