【问题标题】:how to parse xml string from Post method response?如何从 Post 方法响应中解析 xml 字符串?
【发布时间】:2011-08-17 09:09:53
【问题描述】:

我有一个从 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 方法以让它们解析出来并绑定到 listBox。或者无论如何,获得相同的结果会很棒。

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;
}

【问题讨论】:

    标签: data-binding windows-phone-7 xml-parsing


    【解决方案1】:

    你的方法是逆逻辑。你知道你可以在方法上有返回值,对吧?-)

    您需要做的是让您的ParseXmlString 方法将responseString 作为参数,并让它返回创建的IEnumerable,如下所示:

    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"),
            };
    }
    

    并更改您的异步代码处理,以在读取 responseString 完成后对您的 UI 线程执行回调。 然后,在你的 UI 线程上,你会这样做:

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

    如果你需要对回调有一些基本的了解,可以看这个答案:Callbacks in C#

    【讨论】:

    • 在哪里添加 GetResponseAsync(... 到?
    • 正如我所说,您需要重新编写异步代码以进行回调,这样您才能真正了解何时完成。 (还请记住,您需要使用Dispatcher,因为 HttpWebRequest 不在 UI 线程上运行)。如果您需要了解回调的工作原理,请阅读最后一个链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2021-06-03
    相关资源
    最近更新 更多