I have this XML:
<Meals>
<Meal>
<Food course="starter">Soup</Food>
<Food course="mains">Fish</Food>
<Food course="dessert">Ice cream</Food>
</Meal>
<Meal>
<Food course="starter">Soup</Food>
<Food course="mains">Chicken</Food>
<Food course="dessert">Cheese</Food>
</Meal>
<Meal>
<Food course="starter">Melon</Food>
<Food course="mains">lamb</Food>
<Food course="dessert">Ice cream</Food>
</Meal>
</Meals>
And this XPath expression:
//Food[@course='dessert' and text() = 'Ice cream']
This finds every dessert course of ice cream. I want to add a new element after each matching element like so:
<Meals>
<Meal>
<Food course="starter">Soup</Food>
<Food course="mains">Fish</Food>
<Food course="dessert">Ice cream</Food>
<Review>enjoyed</Food>
</Meal>
<Meal>
<Food course="starter">Soup</Food>
<Food course="mains">Chicken</Food>
<Food course="dessert">Cheese</Food>
</Meal>
<Meal>
<Food course="starter">Melon</Food>
<Food course="mains">lamb</Food>
<Food course="dessert">Ice cream</Food>
<Review>enjoyed</Food>
</Meal>
</Meals>
How can I do this in C# given that XPath expression? The expression may alter (for example selecting mains instead), and the structure of the XML may vary too. Regardless a new element must be inserted after each matching element.
I know there is a AddAfterSelf method on an XElement, but I don't know how to use that on the collection of XElements the XPath expression may return.