我有以下来自restful服务的xml数据:
<nodeData>
<nodeObject>
<nodeName>Node 1</nodeName>
<nodeChildren>
<nodeObject>
<nodeName>Node 1-1</nodeName>
<nodeChildren>
<nodeObject>
<nodeName>Leaf 1-1-1</nodeName>
</nodeObject>
<nodeObject>
<nodeName>Leaf 1-1-2</nodeName>
</nodeObject>
<nodeObject>
<nodeName>Leaf 1-1-3</nodeName>
</nodeObject>
<nodeObject>
<nodeName>schedule 4.pdf</nodeName>
</nodeObject>
</nodeChildren>
</nodeObject>
<nodeObject>
<nodeName>Node 1-2</nodeName>
<nodeChildren>
<nodeObject>
<nodeName>Leaf 1-2-1</nodeName>
</nodeObject>
</nodeChildren>
</nodeObject>
</nodeChildren>
</nodeObject>
<nodeObject>
<nodeName>Node 2</nodeName>
<nodeChildren>
<nodeObject>
<nodeName>Node 2-1</nodeName>
<nodeChildren>
<nodeObject>
<nodeName>Leaf 2-1-1</nodeName>
</nodeObject>
</nodeChildren>
</nodeObject>
</nodeChildren>
</nodeObject>
......
</nodeData> 因此,我希望获得树数据来填充sliverlight中的treeview。我做了如下工作:
创建一个内部类:
public class nodeObject
{
public string nodeName { get; set; }
public IEnumerable<nodeObject> nodeChildren { get; set; }
}将Linq写为:
void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XDocument xml = XDocument.Parse(e.Result);
var dataSource = (from results in xml.Descendants("nodeObject")
select new nodeObject
{
nodeName = results.Element("nodeName").Value.ToString(),
nodeChildren = this.GetChilden(results)
});
this.dataTree.ItemsSource = dataSource.ToList();
}
}
private IEnumerable<nodeObject> GetChilden(XElement parent)
{
return (from results in parent.Descendants("nodeObject")
select new nodeObject
{
nodeName = results.Element("nodeName").Value.ToString(),
}).ToList<nodeObject>();
}然后运行silverlight应用程序。数据在树状视图中显示为(只有2个级别,有很多重复):
Node 1
Node 1-1
Leaf 1-1-1
Leaf 1-1-2
Leaf 1-1-3
Node 1-2
Leaf 2-1-1
Node 1-1
Leaf 1-1-1
Leaf 1-1-2
Leaf 1-1-3
Node 1-2
Leaf 2-1-1
Node 1-2
Leaf 2-1-1
Node 2
Node 2-1
Leaf 2-1-1
Node 2-1
Leaf 2-1-1但是预期的显示应该是这样的(没有叶子):
Node 1
Node 1-1
Node 1-2
Node 2
Node 2-1或like (包括叶子):
Node 1
Node 1-1
Leaf 1-1-1
Leaf 1-1-2
Leaf 1-1-3
Node 1-2
Leaf 2-1-1
Node 2
Node 2-1
Leaf 2-1-1如何解决这个问题?
发布于 2011-06-02 21:39:00
弄清楚了:这是因为xaml中的绑定问题。Linq查询很好。
https://stackoverflow.com/questions/6190333
复制相似问题