Solved

What is faster LINQ to XML or StringBuilder?

Posted on 2008-10-26
8
1,122 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 62

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 62

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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

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:
Tiggerito 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now