1

I have an XML file as following:

<?xml version="1.0" encoding="utf-8"?>
<files>
    <file name="1">
        <file name="4">
            <file name="9">
            </file> 
        </file> 
    </file>
    <file name="2">
    </file>
    <file name="3">
        <file name="5">
            <file name="7">
            </file>
        </file>
        <file name="6">
        </file>
    </file>
</files>

I want to create an id for each node using the level of each.

1
1,4
1,4,9
2
3
3,5
3,5,7
3,6

an save it as a list. For each parent will be an entry in the list as:

0
  1
  1,4      
  1,4,9
1
  2
2
  3
  3,5
  3,5,7
  3,6

I used the following code:

using System;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main()
    {    
        var doc = XDocument.Load("test2.xml");
        var hierarchy = doc.Descendants("file")
                   .Where(x => x.Element("file") == null)
                   .Select(x => x.AncestorsAndSelf("file")
                                 .Reverse()
                                 .Select(f => (int)f.Attribute("name"))
                                 .ToList());

        var step = hierarchy.Select(a => String.Join(", ", a));
    }
}

This function may be called the flattening a nested nodes/children in XML file. This code generates a list of only the last nodes and that is it(see the output).

1,4,9
2    
3,5,7
3,6

But I rather prefer to have an address/id for each node separately.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Your output appears to contain a hierarchy, but you're creating a flat list. How would you expect that hierarchy to be represented? It's not clear what you're trying to achieve. – Jon Skeet Apr 04 '16 at 06:11
  • @JonSkeet Please see the edited version. –  Apr 04 '16 at 06:23
  • Do you need a method which return path to the given node? Or you need a method which returns all flattened nodes and their path? – Reza Aghaei Apr 04 '16 at 06:32
  • @RezaAghaei, I need a method which returns all flattened nodes and their path. –  Apr 04 '16 at 06:37
  • Use a recursive method. Pass the level into recursive method. See following webpage : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Apr 04 '16 at 06:46

1 Answers1

0

For each parent will be an entry in the list

You can use such query:

var document = XDocument.Load(@"d:\xml.xml");
var list = document.Root
                   .Elements("file")
                   .Select((x, i) => new
                   {
                       Index = i,
                       Nodes = x.DescendantsAndSelf("file")
                               .Select(y => new
                               {
                                   Node = y,
                                   Path = string.Join("-", y.AncestorsAndSelf("file")
                                                .Reverse()
                                                .Select(f => f.Attribute("name").Value))
                               })
                   }).ToList();

This creates such result:

Index | Node | Path
---------------------
0     | 1    | 1
      | 4    | 1-4
      | 9    | 1-4-9
1     | 2    | 2
2     | 3    | 3
      | 5    | 3-5
      | 7    | 3-5-7
      | 6    | 3-6

I need a method which returns all flattened nodes and their path.

You can use such query:

var document = XDocument.Load(@"d:\xml.xml");
var list = document.Root.Descendants("file")
                   .Select(x => new
                   { 
                       Node= x,  
                       Path = string.Join("-",x.AncestorsAndSelf("file")
                                    .Reverse().Select(f => f.Attribute("name").Value))
                   }).ToList();

This creates a list containing node and path:

Node | Path
-------------
1    | 1
4    | 1-4
9    | 1-4-9
2    | 2
3    | 3
5    | 3-5
7    | 3-5-7
6    | 3-6
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398