Solved

What is faster LINQ to XML or StringBuilder?

Posted on 2008-10-26
8
1,131 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# LINQ 5 40
Load XML element 3 41
VS C# how to design libary of functions 7 27
unable to isntall .net framework 3.5 19 22
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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