0

I'm fairly new to LINQ but this seemed pretty straightforward.

I have an XML doc which contains a structure like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<folders>
  <folder id="-1" parent="-100">
    <name><![CDATA[Root]]></name>
    <children>
      <folder id="2" parent="-1">
        <name><![CDATA[Contribution]]></name>
        <documents />
        <children>
          <folder id="775" parent="2">
            <name><![CDATA[category1]]></name>
            <documents />
            <children>
              <folder id="2319" parent="775">
                <name><![CDATA[Acad_Depts1]]></name>
                <documents />
                <children>
                  <folder id="26965" parent="2319">
                    <name><![CDATA[Student1]]></name>
                    <documents>
                      <document>
                      </document>
                    </documents>
                  </folder
                </children>
              </folder>
              <folder id="2319" parent="775">
                <name><![CDATA[Acad_Depts2]]></name>
                <documents />
                <children>
                  <folder id="26965" parent="2319">
                    <name><![CDATA[Student1]]></name>
                    <documents>
                      <document>
                      </document>
                    </documents>
                  </folder
                </children>
              </folder>
              etc...
            </children>
          </folder>
        </children>
      </folder>
    </children>
  </folder>
</folders>

What I'm trying to do is to select all the elements with an attribute 'parent="775"'.

XElement xelement = XElement.Load("folders_only_registrar_folder.xml");
IEnumerable <XElement> folders = xelement.Elements();

var query = from node in folders.Descendants("folder")
            where node.Attribute("parent").Value == registrarNodeID
            select node;

Console.WriteLine(query.Count());
Console.ReadKey();

foreach(XElement departmentNode in query.Descendants("name"))
{
          Console.WriteLine(departmentNode.Value.ToString());
}

When I run the query and test the count, I get 48 results (which is good)... but when I try to write out those same nodes, I get hundreds of results. For some reason it's giving me almost ALL of the elements named "folder" including children folders.

Thoughts as to what I'm doing wrong?

UPDATE... ok so now I know why i'm getting all the folders but any thoughts on how to create a collection of each grouping of nodes and sub-nodes?

Can the selection in LINQ send each 775 folder node (plus it's collective sub-nodes) into some sort of collection of nodes and then I could parse through them in a foreach by grouping of node?

3
  • where node.Attribute("parent").Value == registrarNodeID is not taken into consideration in your for each loop Jan 28, 2019 at 19:19
  • If you want the immediate name node of each folder node with that parent attribute value then you need to select them select node.Element("name")
    – juharr
    Jan 28, 2019 at 19:23
  • @preciousbetine yes i'm realizing that now also ... i guess what i was hoping to do was to create a collection of 'folder' (plus it's children) nodes so that I could iterate through them... each folder node with a parent attribute of 775 is in essence a Department collection of document details. I need to be able to parse each of these departments on their own, dealing with the specific documents under them as needed. Any suggestions for a better process?
    – K. Ong
    Jan 28, 2019 at 19:27

1 Answer 1

3

Replace query.Descendants() with just query. query.Descendants() gets every child of every node that was originally contained within query.

1
  • 1
    Thanks! That definitely helped... had to add some additional filtering in the console.writeline in that loop but I'm getting my 48 results now. Thanks for the keen eye!!
    – K. Ong
    Jan 28, 2019 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.