I ran a few test using your standard string method, the StringBuilder class method and a method utilising the XDocument and XElement classes. With short lists the StringBuilder class won but with larger lists the XML classes gave comparable times. The test code I used is below:
List<string> list;
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
protected void Page_Load(object sender, EventArgs e)
{
list = Enumerable.Range(0, 15000).Select(i => i.ToString()).ToList();
UsingStrings();
UsingStringBuilder();
UsingXDocument();
}
private void UsingStrings()
{
timer.Reset();
timer.Start();
string beginItemNode = "<Node>";
string endItemNode = "</Node>";
string xml = "<Root>";
foreach (string item in list)
{
xml += beginItemNode + item + endItemNode;
}
xml += "</Root>";
timer.Stop();
Response.Write(string.Format("Strings time:{0}<br />", timer.Elapsed.Ticks));
}
private void UsingStringBuilder()
{
timer.Reset();
timer.Start();
StringBuilder sb = new StringBuilder();
sb.Append("<Root>");
foreach (string item in list)
{
sb.AppendFormat("<Node>{0}</Node>", item);
}
sb.Append("</Root>");
timer.Stop();
Response.Write(string.Format("StringBuilder time:{0}<br />", timer.Elapsed.Ticks));
}
private void UsingXDocument()
{
timer.Reset();
timer.Start();
XDocument xDoc = new XDocument();
xDoc.Add(new XElement("Root"));
foreach (var item in list)
{
XElement element = new XElement("Node", item);
xDoc.Root.Add(element);
}
timer.Stop();
Response.Write(string.Format("XDocument time:{0}<br />", timer.Elapsed.Ticks));
}
The above test gave the following results on my machine:
First run:
Strings time:239750613
StringBuilder time:55509
XDocument time:61904
Second run:
Strings time:289422753
StringBuilder time:198595
XDocument time:80032
If you want to display the XDocument XML as a string without formatting use:
string xml = xDoc.ToString(SaveOptions.DisableFormatting);
EDIT: I initially said XDocument was much faster but running the test a few times it seems that it is generally equivalent to the StringBuilder method although it appears to be more consistent whereas sometimes StringBuilder is faster, other times a fair bit slower. What is clear is that using strings is much slower in comparison.