KentZhou
KentZhou

Reputation: 25573

How to use Linq to Xml to get tree data with recursive querying?

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?

Upvotes: 1

Views: 946

Answers (1)

KentZhou
KentZhou

Reputation: 25573

Figured it out: it is because of binding issue in xaml. Linq query is fine.

Upvotes: 1

Related Questions