Insert xelements using LINQ Select?
- by Simon Woods
I have a source piece of xml into which I want to insert multiple elements which are created dependant upon certain values found in the original xml
At present I have a sub which does this for me:
<Extension()>
Public Sub AddElements(ByVal xml As XElement, ByVal elementList As IEnumerable(Of XElement))
For Each e In elementList
xml.Add(e)
Next
End Sub
And this is getting invoked in a routine as follows:
Dim myElement = New XElement("NewElements")
myElement.AddElements(
xml.Descendants("TheElements").
Where(Function(e) e.Attribute("FilterElement") IsNot Nothing).
Select(Function(e) New XElement("NewElement", New XAttribute("Text", e.Attribute("FilterElement").Value))))
Is it possible to re-write this using Linq syntax so I don't need to call out to the Sub AddElements but could do it all in-line
Many Thx
Simon