我从restful service获得了以下类型的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中填充树视图。我做了如下:
创建内部类:
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
或者喜欢(包括叶子):
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
如何解决此问题?
答案 0 :(得分:1)
想出来:这是因为xaml中的绑定问题。 Linq查询很好。