I have a code here that generates XML for Engineer list using linq. My question is that is there any ways to improve and speed up this method
public static string CreateXMLforEngineersByLinq(List<Engineers> lst)
{
string x = "<Engineers>\n";
x += string.Concat(lst.Select(s =>
string.Format("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n",
s.LicenseID, s.LastName, s.FirstName, s.MiddleName)));
return x + "</Engineers>";
}
RESULTS:
Hi, The code below is the added for me to show the actual speed of the methods i produced more answers and revisions are welcome thanks again for those who people who help here 🙂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using test.Classes;
namespace test.LINQ
{
public static class BatchOperations
{
delegate string Operations(List<Engineers> i);
public static List<int> BatchAddition(List<int> lstNumbers)
{
lstNumbers = (from nl in lstNumbers
select nl + 2).ToList();
return lstNumbers;
}
public static string CreateXMLforEngineersTheSimpleWay(IEnumerable<Engineers> lst)
{
StringBuilder x = new StringBuilder();
x.AppendLine("<Engineers>");
foreach (var s in lst)
{
x.AppendFormat("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n",
s.LicenseID,
s.LastName,
s.FirstName,
s.MiddleName);
}
x.AppendLine("</Engineers1>");
return x.ToString();
}
public static string CreateXMLforEngineersByLinq(List<Engineers> lst)
{
string x = "<Engineers>\n";
x += string.Concat(lst.Select(s =>
string.Format("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n",
s.LicenseID, s.LastName, s.FirstName, s.MiddleName)));
return x + "</Engineers2>";
}
public static string CreateXMLforEngineersByLoop(List<Engineers> lst)
{
string XmlForEngineers = "<Engineers>";
foreach (Engineers item in lst)
{
XmlForEngineers += string.Format("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n"
, item.LicenseID, item.LastName, item.FirstName, item.MiddleName);
}
XmlForEngineers += "</Engineers3>";
return XmlForEngineers;
}
public static void ShowEngineersByLicense()
{
List<Engineers> lstEngr = new List<Engineers>();
Engineers tom = new Engineers();
tom.FirstName = "Tom";
tom.MiddleName = "Brook";
tom.LastName = "Crook";
tom.LicenseID = "1343-343434";
Engineers ken = new Engineers();
ken.FirstName = "ken";
ken.MiddleName = "Brook";
ken.LastName = "Crook";
ken.LicenseID = "1343-343434";
Engineers ben = new Engineers();
ben.FirstName = "ben";
ben.MiddleName = "Brook";
ben.LastName = "Crook";
ben.LicenseID = "1343-343434";
for (int y = 0; y <= 1000; y++)
{
lstEngr.Add(tom);
lstEngr.Add(ken);
lstEngr.Add(ben);
}
List<Operations> j = new List<Operations>();
j.Add(a => CreateXMLforEngineersTheSimpleWay(lstEngr));
j.Add(i => CreateXMLforEngineersByLinq(lstEngr));
j.Add(i => CreateXMLforEngineersByLoop(lstEngr));
DateTime start, end;
TimeSpan diff1 = new TimeSpan();
foreach (Operations currentMethod in j)
{
start = DateTime.Now;
Console.Write(currentMethod(lstEngr));
end = DateTime.Now;
diff1 = end - start;
Console.WriteLine(diff1);
Console.Write("\n\n");
}
}
}
}
All of the other examples are using string concat to generate your XML. The downside of this is that it will fail if any of your values are not properly escaping unsupported characters in the XML (like < and &). It is better to work more natively with the XML. Consider the following:
IF you don’t want to take the memory overhead of creating the entire XML in memory before pushing it out to the string, you might want to consider using the XStreamingElement ( http://msdn.microsoft.com/en-us/library/system.xml.linq.xstreamingelement.aspx ). See http://www.microsoft.com/uk/msdn/nuggets/nugget/295/LINQ-to-XML-Streaming-Large-Data-Files-Out-of-Memory.aspx for a quick video of how to use it.