sksmiths Posted March 3, 2009 Posted March 3, 2009 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 Quote
Administrators PlausiblyDamp Posted March 4, 2009 Administrators Posted March 4, 2009 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Diesel Posted March 5, 2009 Posted March 5, 2009 How much flexibility do you need? Is XSL-T an option. IME xsl-t's are the fastest replacement option. XmlDocument's 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. It's the easiest to maintain, uses the least memory and is fastest with small sets. Quote
Nate Bross Posted March 6, 2009 Posted March 6, 2009 I'd say KIS and use StringBuilder if all you need is simple text replacement; however, if you need to produce standards complient output; the XmlDocument may be a good option. I don't know if this would apply in your situation, but would an approach like this help you: http://haacked.com/archive/2006/11/29/Express_Yourself_With_Custom_Expression_Builders.aspx Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.