0

I have a xml string that return from Post method:

private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

    // End the operation
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
    HttpStatusCode rcode = response.StatusCode;

    var stream = new GZipInputStream(response.GetResponseStream());
    using (StreamReader reader = new StreamReader(stream))
    {
        responseString = reader.ReadToEnd();                
    }          

    response.Close();
}

The responseString is the string I want to parse, using parseXmlString class below. However I can't call the method parseXmlString directly because of the static. How can I pass the responseString to the parseXmlString method to have them parse out and bind to the listBox. Or anyway to have the same result would be great.

void parseXmlString()
{
    byte[] byteArray = Encoding.UTF8.GetBytes(responseString);
    MemoryStream str = new MemoryStream(byteArray);
    str.Position = 0;

    XDocument xdoc = XDocument.Load(str);

    var data = from query in xdoc.Descendants("tracks").Elements("item")
               select new searchResult
               {
                   artist = (string)query.Element("artist"),
                   album = (string)query.Element("album"),
                   track = (string)query.Element("track"),
                   //   artistA = (string)query.Element("artists").Element("artist"),
               };
    //  ListBox lb = new ListBox();

    listBox1.ItemsSource = data;

    var data1 = from query in xdoc.Descendants("artists").Elements("item")
                select new searchResult
                {
                    artistA = (string)query.Element("artist"),
                };
    listBox2.ItemsSource = data1;
}
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
Nghia Nguyen
  • 2,585
  • 4
  • 25
  • 38

1 Answers1

1

Your approach is inversed logic. You know that you can have return values on methods, right?-)

What you need to do is let your ParseXmlString method take the responseString as a parameter, and let it return the created IEnumerable, like this:

private IEnumerable<SearchResult> ParseXmlString(responseString)
{
    XDocument xdoc = XDocument.Load(responseString);

    var data =
        from query in xdoc.Descendants("tracks").Elements("item")
        select new SearchResult
        {
           Artist = (string)query.Element("artist"),
           Album = (string)query.Element("album"),
           Track = (string)query.Element("track"),
        };

    return
        from query in xdoc.Descendants("artists").Elements("item")
        select new SearchResult
        {
           ArtistA = (string)query.Element("artist"),
        };
}

And change your async code handling, to perform a callback to your UI thread, when it's done reading out the responseString. Then, on your UI thread, you would do:

// This being your method to get the async response
GetResponseAsync(..., responseString =>
{
    var searchResults = ParseXmlString(responseString);
    listBox2.ItemsSource = searchResults;
})

You can see this answer, if you need some basic understanding of callbacks: Callbacks in C#

Community
  • 1
  • 1
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • where do I add the GetResponseAsync(... to? – Nghia Nguyen Aug 17 '11 at 09:38
  • As I said, you need to rework your async code to take a callback, so you can actually be informed of when it's done. (Also remember, you'll need to use the `Dispatcher`, as the HttpWebRequest doesn't run on the UI thread). Read the last link, if you need to understand how callbacks work. – Claus Jørgensen Aug 17 '11 at 09:43