How to create a top level xelement with multiple xelement nodes in a foreach loop



I am looking to create a top level XElement with multiple XElement child nodes as below.



<Names>
<Name>
<firstname>Mike</firstname>
<lastname>Smith</lastname>
</Name>
<Name>
<firstname>Sam</firstname>
<lastname>Smith</lastname>
</Name>
<Name>
<firstname>Michelle</firstname>
<lastname>Smith</lastname>
</Name>
</Names>


If I go with something similar to below, how can I append the child elements ( elements) to a top level element ? Thanks for any suggestions.



for (int i = 0; i < 10; i++)
{
var str = Getelement(i);
//str is a string that contains xml
XDocument doc = XDocument.Parse(str);
XElement root = doc.Root;
//root will contain <Name>
}

No comments:

Post a Comment