I have always used XElement.Parse() and StringBuilder to create an XElement of a required type. Today I came across this post on some blog and then this thread on stackoverflow which show how to do the same in a more efficient manner. Sharing it here hope this helps.

Requirement 1:

<students>
    <student>1</student>
    <student>2</student>
    <student>3</student>
    <student>4</student>
    <student>5</student>
</students>

Code:

List <int> numbers = new List <int> () {
    1, 2, 3, 4, 5
};
XElement element = new XElement("students", numbers.Select(i = > new XElement("student", i)));

Requirement 2:

<students>
    <student number="1"></student>
    <student number="2"></student>
    <student number="3"></student>
    <student number="4"></student>
    <student number="5"></student>
</students>

C# Code:

List <int> numbers = new List <int> () {
    1, 2, 3, 4, 5
};
var xelement = new XElement("students");
foreach(var item in numbers) {
    xelement.Add(new XElement("student", new XAttribute("number", item)));
}