Insert xelements using LINQ Select?

Posted by Simon Woods on Stack Overflow See other posts from Stack Overflow or by Simon Woods
Published on 2010-04-27T14:15:16Z Indexed on 2010/06/14 11:42 UTC
Read the original article Hit count: 335

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about Xml

Related posts about LINQ