0

Having a little bit of an issue with using XElement/LINQ to add a new element, when the path is unknown, and am fairly new to LINQ as well. In C#, Winforms

Edit: The 'ID' values in the xml are actually GUID's, just trimmed down here for ease of reading.

For example, if I have the below xml file:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Structure>
    <Nodes>
      <Node ID="1" Name="A" />
      <Node ID="2" Name="B" />
      <Node ID="3" Name="C" >
        <Nodes>
          <Node ID="4" Name="D" />
          <Node ID="5" Name="E" >
            <Nodes>
              <Node ID="6" Name="F" />
            </Nodes>
          </Node>
        </Nodes>
      </Node>
    </Nodes>
  </Structure>
</Root>

Then my code snippet for inserting a new node (ID = 7 Name = G) underneath Node 2 / B is: (Note: I am previously identifying the target, in this case 2, hardcoded for brevity)

// Add Node:
_XML_Modify.Element("Structure")
.Elements("Nodes")
.Elements("Node")
.Where(item => item.Attribute("ID").Value == 2).FirstOrDefault()
.AddAfterSelf(new XElement("Node",
new XAttribute("ID", 7),
new XAttribute("Name", "G")
));

But how can I add a new node, if I am not sure of the target? If for example to add the new node underneath node 4/D or 6/F where the path is different then the above.

Is it best to use recursion to find my target node in this scenario? I've tried that and struggled a little bit, but am thinking of doing a "Find", then if it has child nodes, call recursively until the target node is found.

3
  • Does your ID property have unique values? Commented Sep 16, 2014 at 7:41
  • Yes. The 'ID' values in the xml are actually GUID's, just trimmed down for ease of reading. Commented Sep 16, 2014 at 21:05
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Sep 16, 2014 at 21:18

1 Answer 1

1

If I understand this correctly, you can use Descendants() to find a node without knowing the exact path of the node. For example, to add the new node after node with ID=6 :

_XML_Modify.Descendants("Node")
           .FirstOrDefault(o => (int)o.Attribute("ID") == 6)
           .AddAfterSelf(new XElement("Node",
                            new XAttribute("ID", 7),
                            new XAttribute("Name", "G")
                            ));

Note that this approach may be slower compared to using more specific path, especially given large XML document, because Descendants() will attempt to find matched node(s) within all branches.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works great based on tests on a small xml file. Will see what performance is like when trying to manipulate a lot larger file. And then of course the next step is to remove them as well.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.