?
Solved

What is faster LINQ to XML or StringBuilder?

Posted on 2008-10-26
8
Medium Priority
?
1,180 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 180 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 180 total points
ID: 22809399
So you are not using XML technology to format the XML strings?
0
Technology Partners: 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 180 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 195 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

615 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