如何使用Linq to Xml通过递归查询获取树数据?
我有来自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 中的树视图。我做了如下操作:
创建一个内部类:
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
如何解决这个问题?
I have following kind of xml data from restful service:
<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>
So I want to get the tree data to fill a treeview in sliverlight. What I did as below:
Create a internal class:
public class nodeObject
{
public string nodeName { get; set; }
public IEnumerable<nodeObject> nodeChildren { get; set; }
}
Write Linq as:
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>();
}
then run the silverlight app. the data display in the treevew as(only 2 levels with many duplication):
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
But the expected display should be like (without leaf):
Node 1
Node 1-1
Node 1-2
Node 2
Node 2-1
Or like (include leaf):
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
How to resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了:这是因为 xaml 中的绑定问题。 Linq 查询没问题。
Figured it out: it is because of binding issue in xaml. Linq query is fine.