我正在尝试使用LINQ在外部XML页面中执行数据的异步返回,然后在每个循环上执行一个for每个循环(我希望为每个项目创建一个新的页面,因此在myData、新页面、新按钮、新标签等中的项)
我想知道将数据输入到哪种最佳类型中,以便更容易地执行foreach循环,以及如何做到这一点?
这是我的XML示例。在LINQ中,我知道我需要使用名称空间,并且知道如何获得这些名称空间。
<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; });
}
}
}发布于 2011-04-27 12:47:20
您可以简单地在BackgroundWorker中进行加载。
https://stackoverflow.com/questions/5802528
复制相似问题