总述
调用GetStore可将店铺中所有的Category按level返回。因为不同level间的Category错综复杂,需要使用递归算法进行检索。
详述
此C#例程使用了.NET SDK,用于检索店铺中所有的Category:
using eBay.Service.Core.Sdk;
using eBay.Service.Core.Soap;
using eBay.Service.Call;
using eBay.Service.Util;
namespace NETSDKSample
{
public class SDKSample
{
private void GetStore()
{
//set your credentials for the call
ApiContext context = new ApiContext();
context.ApiCredential.ApiAccount.Developer = "devID";
context.ApiCredential.ApiAccount.Application = "appID";
context.ApiCredential.ApiAccount.Certificate = "certID";
context.ApiCredential.eBayToken = "token";
// Set the URL
context.SoapApiServerUrl ="https://api.sandbox.ebay.com/wsapi";
// Set logging
context.ApiLogManager = new ApiLogManager();
context.ApiLogManager.ApiLoggerList.Add(neweBay.Service.Util.FileLogger("Messages.log", true, true, true));
context.ApiLogManager.EnableLogging = true;
// Set the version
context.Version = "571";
//create call
GetStoreCall call = new GetStoreCall(context);
//get just the store categories
call.CategoryStructureOnly = true;
call.Execute();
//iterate through the top level categories
foreach (StoreCustomCategoryType cat incall.Store.CustomCategories)
{
GetChildCategories(cat);
}
}
private void GetChildCategories(StoreCustomCategoryType cat)
{
//get the category name, ID and whether it is a leaf
long id = cat.CategoryID;
string name = cat.Name;
bool leaf = (cat.ChildCategory.Count == 0);
Console.WriteLine("id = " + id + " name = " + name + " leaf= "+ leaf);
//condition to end the recursion
if (leaf)
{
return;
}
//continue the recursion for each of the child categories
foreach (StoreCustomCategoryType childcat in cat.ChildCategory)
{
GetChildCategories(childcat);
}
}
}
}
|