Adding new xElement after ALL found Descendants

90 views Asked by At

I have an xDocument with multiple various xElements.

I can successfully find a specific xElement by searching via it's xAttributes & then Add a new xElement after it using the code below:

xDocument.Descendants("td").LastOrDefault(e => ((string)e.Attribute("ID")) == "3").Add(new XElement("b", "Just a test."));

The problem is that I wish to Add this new xElement after all found instances of the Descendants, not just LastOrDefault or FirstOrDefault.

My xDocument is created dynamically & there is no way before hand to know how many 'td' xElements with 'ID' = '3' that there are going to be.

Any help would be appreciated. Thanks


ADDED CODE AS REQUESTED

<html> .... etc....  
    <body> 
    <table> 
    <tr> 
    <td>Image</td>
    <td>Description</td> 
    <td>Date</td> 
    </tr> 
    <tr> 
    <td ID="1">*.jpg</td>
    <td ID="2">some image</td>
    <td ID="3">01/01/1901</td>  <--CHANGING THIS PART OF CODE--> 
    <--THIS TABLE ROW REPEATS AN UNDETERMINED NUMBER
    OF TIMES RELATING TO THE NUMBER OF FILES CONTAINED IN WHATEVER DIRECTORY IS BEING SEARCHED USING A FOREACH LOOP IN ANOTHER PART OF
    THE CODE--> 
    </tr> 
    </table> 
    </body> 
    </html>

So I am trying to add a tag between the <td> with ID = 3. This <b> tag also contains a string variable i.e. new xElement("b", DateTaken) & needs to be created at runtime and not hard coded as it relates to each loaded image at the start of the table row.

So I am trying to add this <b> tag to every occurrence of <td> with ID=3 & not just the first or the last.

Hope this extra info helps.

0

There are 0 answers