You can use Linq-to-XML
List Branches = new List();
Branches.Add(1);
Branches.Add(2);
Branches.Add(3);
var branchesXml = Branches.Select(i => new XElement("branch",
new XAttribute("id", i)));
var bodyXml = new XElement("Branches", branchesXml);
System.Console.Write(bodyXml);
Or create the appropriate class structure and use XML Serialization.
[XmlType(Name = "branch")]
public class Branch
{
[XmlAttribute(Name = "id")]
public int Id { get; set; }
}
var branches = new List();
branches.Add(new Branch { Id = 1 });
branches.Add(new Branch { Id = 2 });
branches.Add(new Branch { Id = 3 });
// Define the root element to avoid ArrayOfBranch
var serializer = new XmlSerializer(typeof(List),
new XmlRootAttribute("Branches"));
using(var stream = new StringWriter())
{
serializer.Serialize(stream, branches);
System.Console.Write(stream.ToString());
}