Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6724039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:38:26+00:00 2026-05-26T09:38:26+00:00

I have a code here that generates XML for Engineer list using linq. My

  • 0

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");
        }
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:38:27+00:00Added an answer on May 26, 2026 at 9:38 am

    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:

    public static string CreateXMLforEngineersByLinq(List<Engineers> lst) 
    { 
        string x = New XElement("Engineers", 
                       lst.Select(new XElement, "Engineer",
                          new XElement("LicenseID", s.LicenseID),
                          new XElement("LastName", s.LastName),
                          new XElement("FirstName", s.FirstName),
                          new XElement("MiddleName", s.MiddleName)
                       )
                   );
        return x.ToString();
    } 
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some working Javascript code that generates an RDF/XML document using variables picked
We have some old C code here that's built with nmake. Is there an
I have a piece of code here that does not work despite me using
I have some code that is using SyncEnumerator. As you can see here ,
I have some Java code that validates XML against an XSD. I am using
I have a piece of code here that i really could use some help
enter code here Hi All, I have a simple windows service application that connects
So I have some PHP code that looks like: $message = 'Here is the
I have this html code that i want to edit with jQuery. Here is
Here's the c# code that I have: private double get806Fees (Loan loan) { Loan.Fee.Items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.