0
我有一個XML字符串,從Post方法返回:如何從Post方法響應中解析xml字符串?
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();
}
的responseString是我想分析該字符串,用下面parseXmlString類。但是,由於靜態,我無法直接調用parseXmlString方法。我怎樣才能將responseString傳遞給parseXmlString方法來解析並綁定到列表框。或者無論如何,有相同的結果會很好。
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;
}
我在哪裏添加GetResponseAsync(...什麼? –
正如我所說的,你需要重做你的異步代碼來進行回調,這樣你就可以知道它何時完成了。(還記得,你需要使用Dispatcher,因爲HttpWebRequest不運行在UI線程上)。閱讀最後一個鏈接,如果你需要了解回調是如何工作的。 –