首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Linq to Xml进行递归查询来获取树数据?

如何使用Linq to Xml进行递归查询来获取树数据?
EN

Stack Overflow用户
提问于 2011-06-01 00:02:14
回答 1查看 927关注 0票数 1

我有以下来自restful服务的xml数据:

代码语言:javascript
复制
<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。我做了如下工作:

创建一个内部类:

代码语言:javascript
复制
public class nodeObject
    {
        public string nodeName { get; set; }
        public IEnumerable<nodeObject> nodeChildren { get; set; }
    }

将Linq写为:

代码语言:javascript
复制
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个级别,有很多重复):

代码语言:javascript
复制
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

但是预期的显示应该是这样的(没有叶子):

代码语言:javascript
复制
Node 1
  Node 1-1
  Node 1-2
Node 2
  Node 2-1

或like (包括叶子):

代码语言:javascript
复制
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

如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2011-06-02 21:39:00

弄清楚了:这是因为xaml中的绑定问题。Linq查询很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6190333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档