1.我想要
从 xml 文件中获取正确的 xml 元素
XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<tasks>
<task>
<title>Test0001</title>
<due>06/17/2012</due>
</task>
<task>
<title>Test0002</title>
<due>06/17/2012</due>
</task>
<task>
<title>Test0003</title>
<due>06/17/2012</due>
</task>
<tasks>
2.我编码的内容
Linq 块:
StorageFile file1 = await ApplicationData.Current.LocalFolder.GetFileAsync("BetterTask.xml");
XDocument doc1 = new XDocument();
using (var ReadStream1 = await file1.OpenStreamForReadAsync() as Stream)
{
doc1 = XDocument.Load(ReadStream1);
}
var data = from query in doc1.Descendants("task")
select new mytask
{
title = (string)query.Element("title"),
due = (string)query.Element("due")
};
List<mytask> myTasks = data.ToList();
myTodayListBox.ItemsSource = myTasks;
mytask 类
public class mytask
{
public string title { get; set; }
public string due { get; set; }
}
3.我得到
的是我在块的最后设置了一个中断,当应用程序中断时,我在 LinQ 部分发现:
<1>[doc1] 充满了 XML 内容
<2>但是 [myTasks] 是空的(计数 = 0),....
4. 为什么以及如何解决
:(