Skip to main content
Published: November 11 2010, 6:30:00 PMUpdated: September 10 2022, 12:10:13 AM

 

Code snippet that demonstrates finding a product match in the eBay catalog by title. The code uses the Shopping API call FindProducts. In case multiple matching products are found, the top 3 in the response are returned.

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.IO;
using System.Xml;

namespace eBayProductSearch
{
    public class eBayProdSearch

    {
         private void searcgProdByTitle
        {

            string Title = "LG 42" Plasma TV 42PQ3000"
            string AppID = "YourAppID";
            string error;

            //make the FindProducts call
            string request = "http://open.api.ebay.com/shopping?appid=" + AppID + "&version=623&siteid=0&callname=FindProducts&MaxEntries=3&QueryKeywords=" + Title;

            WebRequest wrGETURL;
            wrGETURL = WebRequest.Create(request);
    
            Stream objStream;
            try
            {
                //read the response
                objStream = wrGETURL.GetResponse().GetResponseStream();
                StreamReader objReader = new StreamReader(objStream);
                string strResponse = objReader.ReadToEnd();

                //parse the response
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(strResponse);
                XmlNode root = xmlDoc["FindProductsResponse"];
                string ProdIDs;

                if (root["Errors"] != null)
                {
                    //There is an error
                    error = root["Errors"]["ShortMessage"].InnerText;
                    Console.WriteLine(Title + "\t" + error);
                }
                else
                {
                    //Browse the response for Product matches
                    XmlNodeList ProdList = xmlDoc.GetElementsByTagName("Product");

                    //There could be more than one Product match
                    for (int i = 0; i < ProdList.Count; i++)
                    {
                        ProdIDs = "";

                        //Each Product can have multiple Product identifiers associated.
                        //We want to get all
                        XmlNodeList ProdIDList = ProdList[i].ChildNodes;
                        for (int j = 0; j < ProdIDList.Count; j++)
                        {
                            if (ProdIDList[j].Name == "ProductID")
                            {
                                XmlAttributeCollection attCol = ProdIDList[j].Attributes;
                                ProdIDs += attCol[0].Value + "\t" + ProdIDList[j].InnerText + "\t";
                            }
                        }
                        ProdIDs = ProdIDs.Replace("Reference", "ePID");
                        Console.WriteLine(Title + "\t" + ProdIDs);
                        
                    }

                 }

             }

            catch (Exception ex)
            {
                Console.WriteLine("FindProducts error - identifier = " + Title + "\t" + ex.Message);
                
            }

}

}

}

 

 

How well did this answer your question?
Answers others found helpful

Got thoughts? Click the feedback button – your insights help us improve!