skip to Main Content

I am super stuck on a simple api usage for over a week now. Here are the details.

Trying to make an api call to ebay.com.
Here is what my code looks like…

This is the starting pages code:

 protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Results.aspx?Keywords=" + searchString.Text);  
    }

The page is directed to this bit of code:

if (Request.QueryString["Keywords"] != null){
        string keywords = Request.QueryString["Keywords"];
            string myAppID = "HIDDEN";
            var xml = XDocument.Load("http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=" + myAppID + "&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=" + keywords + "&paginationInput.entriesPerPage=5");
            XNamespace ns = "http://www.ebay.com/marketplace/search/v1/services";
            var titles = from item in xml.Root.Descendants(ns + "title")
                              select new{
                                  title = xml.Descendants(ns + "title").Select (x => x.Value),
                              };
        foreach (var item in titles){
                Label1.Text += item;
            } 
        }

Here is the xml example:

<findItemsByKeywordsResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
<searchReslut count="5">
<item>
    <title></title>
</item>
<item>
    <title></title>
</item>
<item>
    <title></title>
</item>

I really rather turn the items into an array not just list them out. Just thought I would try the easier approach first. Error I get is:
The for loop output to my label looks like this:

{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }{ title = System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String] }

And the output exception is:

A first chance exception of type ‘System.Threading.ThreadAbortException’ occurred in mscorlib.dll
An exception of type ‘System.Threading.ThreadAbortException’ occurred in mscorlib.dll but was not handled in user code
The thread ” (0x27ee4) has exited with code 0 (0x0).

Any help is appreciated!

2

Answers


  1. The exception is coming from:

    var titles = from item in xml.Root.Descendants(ns + "title")
                 select new
                 {
                     title = item.Parent.Element("title").Value,
                 };
    

    You don’t need to go to the parent, you can just directly get the value of title since that is what you are searching for:

    xml.Descendants(ns + "title").Select (x => x.Value)
    

    Also, please take a look here Asking Better Questions, as it’ll likely get you faster/better responses. There is a ton of code in your question that is not relevant, and is hard to parse to get to what you actually need help with.

    Login or Signup to reply.
  2. This should be fairly simple to accomplish. Try using the code below. The top part I am just setting up your sample XML from your example and parsing it to a XElement (note: some closing tags were missing, also I am assuming the ‘searchReslut’ node is supposed to be ‘searchResult’). Next, I am grabbing all of the descendant nodes from the root node that have a name of ‘title’ and where the value is not null. Then I am simply looping through the IEnumerable that is created, and concatenating the text of the label. Let me know if there are any questions.

    string ns = "http://www.ebay.com/marketplace/search/v1/services";
    string returnedXml = "<findItemsByKeywordsResponse xmlns="" + ns + "">" +
                        "<searchReslut count="5">" +
                            "<item>" +
                                "<title>Title1</title>" +
                            "</item>" +
                            "<item>" +
                                "<title>Title2</title>" +
                            "</item>" +
                            "<item>" +
                                "<title>Title3</title>" +
                            "</item>" +
                         "</searchReslut>" +
                        "</findItemsByKeywordsResponse>";
            XElement xml = XElement.Parse(returnedXml);
            IEnumerable<XElement> titleNodes = xml.Descendants("title").Where(x => x.Value != null);
            foreach (XElement t in titleNodes)
            {
                Label1.Text += t.Value;
            }            
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search