C# XML vs StringBuilder

sksmiths

New member
Joined
Mar 3, 2009
Messages
2
Opinions please...

We have a web based application that is template driven. The template, HTML code, contains elements that we want to replace with code-generated HTML snippets (e.g. <FORM/> would be replaced with an HTML <form> Stuff here</form> element created by code).

We have two paths there. First we can load the template into a StringBuilder object and use the .Replace method to perform the string replacement. Second, we can load the template into an XmlDocument object and replace the element using the replaceChild method.

Both are fairly straightforward in nature, so the question is this: is one "better" than the other? Speed? Memory usage? This substitution is going to be used very heavily as each page of the site will be built using this code.

Any suggestions?
Thx
Tim
 
Using an Xmldocument will make it easier to produce valid html / xml markup as it will take care of escaping non-valid character sequences etc.

Performance wise you would probably need to run a couple of comparisons to get a feel for which is going to give the best performance / feature tradeoff.
 
How much flexibility do you need?

Is XSL-T an option. IME xsl-ts are the fastest replacement option.

XmlDocuments are extremely thick objects though, and creating an XslNavigator uses even more memory.

If it really is as simple as replacing text, I would stick with StringBuilder. Its the easiest to maintain, uses the least memory and is fastest with small sets.
 
Back
Top