Linq To Xml: Is it possible to add XElement inline with search
New to Linq to Xml and having a play around with it.
I am carrying out a lot of operations similar to 开发者_JAVA百科the following and I was wondering if it was possible in one statement:
Dim childXEl As XElement = _
<ChildElement></ChildElement>
Dim results = _
From parentXEl In xdoc.Descendants.<ParentXElement>
For Each xe As XElement In results
xe.Add(childXEl)
Next
I doubt this is possible but I am curious to know, can you add the xml in the search query thus making the for each loop unnecessary?
Well List(Of T) has a ForEach method taking an Action you can can do stuff like this:
Sub Main()
Dim doc As XDocument = _
<?xml version="1.0"?>
<root>
<item>
<foo>1</foo>
</item>
<item>
<foo>2</foo>
</item>
</root>
doc.Root.<item>.ToList().ForEach(AddressOf Add)
doc.Save("output.xml")
End Sub
Sub Add(ByVal el As XElement)
el.Add(<bar>test</bar>)
End Sub
精彩评论