How do I remove a child element based on attributes on multiple parents?

crimeainvest

New Member
I have an XML file that will be similar to the structure below:\[code\]<?xml version="1.0" encoding="utf-8"?><Root Attr1="Foo" Name="MyName" Attr2="Bar" > <Parent1 Name="IS"> <Child1 Name="Kronos1"> <GrandChild1 Name="Word_1"/> <GrandChild2 Name="Word_2"/> <GrandChild3 Name="Word_3"/> <GrandChild4 Name="Word_4"/> </Child1> <Child2 Name="Kronos2"> <GrandChild1 Name="Word_1"/> <GrandChild2 Name="Word_2"/> <GrandChild3 Name="Word_3"/> <GrandChild4 Name="Word_4"/> </Child2> </Parent1></Root>\[/code\]The elements are not defined in that they can have different values than other files. What I do know is the "Name" attribute of each element before hand, which will always be defined. I need to be able to manipulate, and/or delete data within a selected element based on that name. For Example: \[code\]removeElement("MyName.IS.Kronos1.Word_1")\[/code\] would delete the \[code\]GrandChild1\[/code\] element underneath the \[code\]Child1\[/code\] Parent. My issues is that while using LINQ to XML queries I'm not able to select that element properly. Using this:\[code\]private IEnumerable<XElement> findElements(IEnumerable<XElement> docElements, string[] names){ // the string[] is an array from the desired element to be removed. // i.e. My.Name.IS ==> array[ "My, "Name", "IS"] IEnumerable<XElement> currentSelection = docElements.Descendants(); foreach (string name in names) { currentSelection = from el in currentSelection where el.Attribute("Name").Value =http://stackoverflow.com/questions/10889071/= name select el; } return currentSelection;}\[/code\]To find where I need to remove the elements yields this result:\[code\]<?xml version="1.0" encoding="utf-8"?><Root Attr1="Foo" Name="MyName" Attr2="Bar" > <Parent1 Name="IS"> <Child1 Name="Kronos1"> <GrandChild2 Name="Word_2"/> <GrandChild3 Name="Word_3"/> <GrandChild4 Name="Word_4"/> </Child1> <Child2 Name="Kronos2"> <GrandChild2 Name="Word_2"/> <GrandChild3 Name="Word_3"/> <GrandChild4 Name="Word_4"/> </Child2> </Parent1></Root>\[/code\]After debugging it appears that all I'm doing is searching for the same document over again, but for different names each time. How do I search and select a specific element based on multiple parent attribute Names?It should be noted, that the size of the XML (meaning levels of elements) are also variable. Meaning that there can as little as 2 levels (Parents) or up to 6 levels (Great-Great-GrandChildren). However, I NEED to be able to look at the root node's \[code\]Name\[/code\] Attribute as well.
 
Back
Top