Adding Xelement into an existing Xelement in foreach loop



For example, If I have a list with two objects and I want to put them into an XML file using Xelement and trying to loop through the objects it put's the second object items into the first Xelement name because they have the same Xelement tagname.


How can I tell an Xelement that it needs to be unique in some kind? So it won't put the second object in the first people tag but in the second.



List<People> people -> has two items

foreach (var person in people)
{
xmlDoc.Element("people").Add(new XElement("person"............

}


example of XML output:



<people>
<person></person>
<person></person>
</people>
<people>
</people>

No comments:

Post a Comment