I'm having problems writing a C# script that'll get me a certain parent element of a given child element. To clear things up, this is an example of the XML tree.
<parent attribute='X'>
<child_element1>A</child_element1>
<child_element2 attribute='Y'>
<grandchild_element1>B</grandchild_element1>
<grandchild_element2>
<key>C</key>
</grandchild_element2>
<grandchild_element3>D</grandchild_element3>
</child_element2>
<child_element3>E</child_element3>
</parent>
Traversing the tree, my script has found the <key>
node. What I'm trying to produce is the following tree.
<parent attribute='X'>
<child_element2 attribute='Y'>
<grandchild_element1>B</grandchild_element1>
<grandchild_element2>
<key>C</key>
</grandchild_element2>
<grandchild_element3>D</grandchild_element3>
</child_element2>
</parent>
As you can see, I'm trying to remove child_element1
and child_element3
in original tree.
I've written a pseudo-code for the script as follows, but so far have failed to figure out how to script the real thing in C#.
- Show all the first and second parent elements above of the key node.
grandchild_element2
is first parent of key andchild_element2
is second parent of key. So all their elements are shown. - Show only node/element that has immediate relation with key for next parent level.
parent
is third parent of key. Theparent
element that has direct relation with key ischild_element2
. So onlychild_element2
is shown.child_Element1
andchild_element2
are not shown because they do not have direct relation with the key
I've taken a look at the following solutions on stackoverflow, but unfortunately they all only show how to get all parent elements (instead of showing the ones which are ancestors of the <key>
node.