Solved

What is faster LINQ to XML or StringBuilder?

Posted on 2008-10-26
8
1,120 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now