Solved

What is faster LINQ to XML or StringBuilder?

Posted on 2008-10-26
8
1,129 Views
Last Modified: 2013-12-17
I have to create a html output files on a windows mobile application. Currently I am using StringBuilder for creating the output files. I was wondering whether i shuld switch to  LINQ to XML. The question is there any advantages like better performance by using  LINQ to XML instead of StringBuilder?
0
Comment
Question by:koleto115
  • 4
  • 3
8 Comments
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 60 total points
ID: 22808085
The two technologies are different. One is a query language and the other is an object that holds a string value.

How are you creating the XML document which is being stored in the StringBuilder object?
0
 

Author Comment

by:koleto115
ID: 22808755
I just simply append  string by using Append and AppendFormat methods.
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 60 total points
ID: 22809399
So you are not using XML technology to format the XML strings?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:koleto115
ID: 22811447
No, I have to render HTML format to a web server on windows mobile app. Since LINQ to XML allow to create simple XML nodes and attributes I can use either LINQ or to create the whole output as a string using StringBuilder and Append methods. The question is is there any advantages of using the LINQ for this scenario such as better speed/memory performance?
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 60 total points
ID: 22812280
Hi koleto115;

Well I have not seen any articles on performance between using Linq to XML and construction using an object such as StringBuilder although I would not expect much of a difference. As far as advantages of using Linq To XML to construct XML nodes over creating them yourself, Linq to XML makes the operation simpler and self documenting. For example the following XML can be created with Linq code:

<books>
    <book>
        <author>John Doe</author>
        <title>Linq To XML Is Great</title>
    </book>
</books>

XElement books = new XElement("books",                        // The Root Node
                                               new XElement("book",             // The book node
                                                   new XElement("author", "John Doe"),                // Child Node
                                                   new XElement("title", "Linq To XML Is Great")  // Child Node
                                               )
                                           )

In the above the both parameters can be a data variable and can be put into a loop to create multiple structures.

Fernando

0
 

Author Comment

by:koleto115
ID: 22812754
I have tried both methods and I got just impression that the StringBuilder is faster but I wanted to be sure about this before I take decision to rewrite all the code.

Thanks
0
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 65 total points
ID: 22820553
I'm .Net 2.0 so don't know linq, but I would assume it would use something as efficient as stringbuilder to generate a string version of its data. (probably a stream writer of some sort).

Linq would add a small overhead to the whole process, however it would also reduce the chances of errors and adds some flexibility in the way you can build your xml.

I'm guessing, as it has XML based classes it will also make sure your data is encoded correctly.

My vote is you stick with Linq.

p.s. It looks like I re-invented linq for 2.0 without realising it...

http://www.mccreath.org.uk/Article/ClockWork-Script-Builder--Net_43.aspx

Will have to look into it!

0
 

Author Comment

by:koleto115
ID: 22824221
Hmm, sounds interesting!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question