<...">

Create a list of nested node attributes from an XML file

I have an XML file:

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

Now I want to create a list of strings / numbers that store the entire attribute namein a list / array with a hierarchy of nested nodes. For example, for the above XML file, the expected list is

(1,4
2
3,5,7)

Because I could know the desired node, at what level.

Could you tell me what you think about such a list?

Update: After Jon, answer, if the child nodes are in the same hierarchy, then it will be as follows.

XML file:

<files>
<file name="1">
    <file name="4"/>     
    <file name="2">
      <file name="3"/>
    </file> 
</file>
<file name="5"/>

and the desired result:

1,4
1,2
1,2,3
5

PS. After testing some examples, I noticed that I should have the first parent, and then the children, if there are two or more children from the same level.

+4
2

, , , :

  • file, file
  • file ancestry
  • name

, , IEnumerable<List<int>>:

var hierarchy = doc.Descendants("file")
                   .Where(x => x.Element("file") == null)
                   .Select(x => x.AncestorsAndSelf("file")
                                 .Reverse()
                                 .Select(f => (int) f.Attribute("name"))
                                 .ToList());

:

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

class Test
{
    static void Main()
    {
        var doc = XDocument.Load("test.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());

        foreach (var item in hierarchy)
        {
            Console.WriteLine(string.Join(", ", item));
        }
    }
}

:

1, 4
2
3, 5, 7

, file, :

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

class Test
{
    static void Main()
    {
        var doc = XDocument.Load("test.xml");
        var hierarchy = doc.Root.Elements("file")
                   .Select(x => x.DescendantsAndSelf("file")
                                 .Select(f => (int) f.Attribute("name"))
                                 .ToList());

        foreach (var item in hierarchy)
        {
            Console.WriteLine(string.Join(", ", item));
        }
    }
}

, "" file. , , XML:

<files>
    <file name="1">
        <file name="4"/>     
        <file name="2">
          <file name="3"/>
        </file> 
    </file>
    <file name="5"/>
</files>

1 - , ?

+3

XElement.DescendantsAndSelf. :

var document = XDocument.Load(@"d:\xml.xml");
var list = document.Root.Elements()
                   .Select(x=>string.Join(",", 
                       x.DescendantsAndSelf().Select(d=>d.Attribute("name").Value)))
                   .ToList();

List<string>, . , IEnumerable<string> , string.Join select.


, . , , AncestorsAndSelf, Jon .

+1

All Articles