In addition to @RichardSchneider's answer, this is sample snippet in VB.
First, construct XDocument
with single <Customers>
element as root. Then in each iteration of For Each
loop, add single <Customer>
element to the root element of the XDocument
:
XDoc As XDocument = <?xml version="1.0" encoding="UTF-16" standalone="yes"?><Customers></Customers>
For Each cust As Customer In customers
XDoc.Root.Add(
<Customer>
<Name>Mike</Name>
<Age>0</Age>
</Customer>
)
Next
XDoc.Save("C:\myXmlfile.xml")