none
How to recursive loop through cild elements?

    Question

  • Hi,

    i have the following xml structure:

    <?xml version="1.0" encoding="utf-8" ?> <Templates> <Template ID="100" Title="Contenttemplate-1"> <Node ID="101">node101 <Node ID="102">node102</Node> <Node ID="106">node106 <Node ID="108">node108</Node> <Node ID="109">node109</Node> </Node> </Node> <Node ID="103">node103</Node> <Node ID="104">node104</Node>

    </Template> </Templates>

    How could i loop recursively through the elements of Template?

    The result should look like:

    node101
      -- node102
      -- node106
      -- -- node108
      -- -- node109
    node103
    node104

    How to solve this?


    Best regards,
    Yavuz B.

    My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog List


    • Edited by [Yavuz B.] Saturday, June 22, 2013 10:43 AM
    Saturday, June 22, 2013 10:43 AM

Answers

  • With C# if you are dealing with recursive XML then you usually write a recursive method, here is an example which takes the XML you have posted and populates a Windows Forms TreeView with a recursive method that takes LINQ to XML XElements and creates Windows  Forms TreeNodes:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                PopulateTreeView(treeView1, XDocument.Load("Template1.xml").Descendants("Template").First());
            }
    
            void PopulateTreeView(TreeView view, XElement template)
            {
                view.Nodes.AddRange((from node in template.Elements("Node") select MakeTreeNode(node)).ToArray());
            }
    
            TreeNode MakeTreeNode(XElement node)
            {
                TreeNode treeNode = new TreeNode((node.FirstNode as XText).Value.Trim());
                treeNode.Nodes.AddRange((from childNode in node.Elements("Node") select MakeTreeNode(childNode)).ToArray());
                return treeNode;
            }

    Of course depending on your needs the recursive method could instead simply write some output for each node passed in and process child nodes with a foreach loop making the recursive call. But for a TreeView respectively for TreeNodes passing in an array created from some LINQ

      (from child in node.Elements("Node") select MyRecursiveFunction(childNode).ToArray()

    is slightly more compact and elegant.


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    • Proposed as answer by Martin Honnen Sunday, June 23, 2013 8:55 AM
    • Marked as answer by [Yavuz B.] Sunday, June 23, 2013 10:44 AM
    Saturday, June 22, 2013 1:41 PM

All replies

  • Do you want to use a .NET language like C# or VB.NET? Or XSLT?

    With XSLT you would use

    <xsl:template match="Template[@ID = '100']">

      <ul>

        <xsl:apply-templates select="Node"/>

      </ul>

    </xsl:template>

    <xsl:template match="Node">

      <li>

         <xsl:value-of select="text()[1]"/>

         <xsl:if test="Node">

            <ul>

                <xsl:apply-templates select="Node"/>

           </ul>

         </xsl:if>

      </li>

    </xsl:template>


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    Saturday, June 22, 2013 11:17 AM
  • Oh, a very good question ;) sorry:

    i am developing under c# the solution could be c#, vb.net, linq or something waht i could use under/convert to c#.


    Best regards,
    Yavuz B.

    My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog List

    Saturday, June 22, 2013 11:20 AM
  • With C# if you are dealing with recursive XML then you usually write a recursive method, here is an example which takes the XML you have posted and populates a Windows Forms TreeView with a recursive method that takes LINQ to XML XElements and creates Windows  Forms TreeNodes:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                PopulateTreeView(treeView1, XDocument.Load("Template1.xml").Descendants("Template").First());
            }
    
            void PopulateTreeView(TreeView view, XElement template)
            {
                view.Nodes.AddRange((from node in template.Elements("Node") select MakeTreeNode(node)).ToArray());
            }
    
            TreeNode MakeTreeNode(XElement node)
            {
                TreeNode treeNode = new TreeNode((node.FirstNode as XText).Value.Trim());
                treeNode.Nodes.AddRange((from childNode in node.Elements("Node") select MakeTreeNode(childNode)).ToArray());
                return treeNode;
            }

    Of course depending on your needs the recursive method could instead simply write some output for each node passed in and process child nodes with a foreach loop making the recursive call. But for a TreeView respectively for TreeNodes passing in an array created from some LINQ

      (from child in node.Elements("Node") select MyRecursiveFunction(childNode).ToArray()

    is slightly more compact and elegant.


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    • Proposed as answer by Martin Honnen Sunday, June 23, 2013 8:55 AM
    • Marked as answer by [Yavuz B.] Sunday, June 23, 2013 10:44 AM
    Saturday, June 22, 2013 1:41 PM