【问题标题】:Asynchronous return of an XML feedXML 提要的异步返回
【发布时间】:2011-08-13 17:41:41
【问题描述】:

我正在尝试使用 LINQ to XML 在外部 XML 页面中执行数据的异步返回,然后执行 for each 循环(我希望为每个项目创建一个新页面,因此 foreach 项目在myData、新页面、新按钮、新标签等)

我想知道最好的数据类型是什么,以便于执行 foreach 循环,以及如何做到这一点?

这是我的 XML 示例。我知道在我的 LINQ to XML 中我需要使用命名空间,并且知道如何获取这些。

<category term="theoryDatabaseModel.questionTable" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
  <m:properties>
    <d:Id>1</d:Id>
    <d:Text>This is sample XML Text</d:Text>
    <d:ImageURL m:null="true" />
    <d:CategoryId>3</d:CategoryId>
  </m:properties>
</content>

我一直在使用以下代码将项目本地获取到列表中,但现在需要异步执行。

List<CategoryFeedItem> catfeedItems = (from categories in docu.Descendants(mm + "properties")
                                            select new CategoryFeedItem()

                                         {
                                             CategoryId = categories.Descendants().ToList()[0].Value,
                                             CategoryText = categories.Descendants().ToList()[1].Value,

                                         }).ToList();

提前致谢

编辑: 我已经使用了这个可行的方法,并将列表带回列表框中。 我可以用 forEach 以某种方式解析这些数据以将其逐项分解吗? 我也会看看 BackgroundWorker,谢谢。

public class CategoryFeedItem
{

    public string CategoryId { set; get; }
    public string CategoryText { set; get; }

}

public class CategoryFeed
{

    ListBox myCatContext;

    public void LoadCatFeed(ListBox context)
    {
        myCatContext = context;
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://mydatasource.svc"));
        request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
    }

    private static readonly XNamespace mm = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

    private void ReadCallback(IAsyncResult asynchronousResult)
    {


        HttpWebRequest request =
          (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response =
          (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        XDocument docu = XDocument.Load(response.GetResponseStream());

        List<CategoryFeedItem> catfeedItems = (from categories in docu.Descendants(mm + "properties")
                                            select new CategoryFeedItem()

                                         {
                                             CategoryId = categories.Descendants().ToList()[0].Value,
                                             CategoryText = categories.Descendants().ToList()[1].Value,
                                         }).ToList();

        myCatContext.Dispatcher.BeginInvoke(() => { myCatContext.ItemsSource = catfeedItems; });
        }

    }
}

【问题讨论】:

  • 你能澄清你想要达到的目标吗?您是只想异步运行整个过程(填充列表),还是希望在添加每个项目时收到通知?
  • 整个过程,不需要通知每一项。我只需要获取列表,以便我可以开始查询它,如果列表是最好的方法吗?谢谢

标签: c# windows-phone-7 linq-to-xml


【解决方案1】:

您可以简单地在BackgroundWorker 中进行加载。

【讨论】:

  • 谢谢你,我写了类似的东西,但可能会看到 BackgroundWorker 可以做什么
  • @beat145 根据您更新的问题。如果您在 HttpWebRequest 回调中填充您的列表,那么它已经在异步运行。您可能希望在 BackgroundWorker 中调用 LoadCatFeed,否则阻塞 UI 会阻止它处理响应。
猜你喜欢
  • 2015-04-07
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多