Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 726425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:27:02+00:00 2026-05-14T06:27:02+00:00

It feels dirty. But maybe it isn’t… is it ok to use a StringBuilder

  • 0

It feels dirty. But maybe it isn’t… is it ok to use a StringBuilder for writing XML? My gut instinct says “although this feels wrong, it’s probably pretty darn performant because it’s not loading extra libraries and overhead it’s not doing whatever extra method calls XmlWriter invokes.” It also seems like it’s just less code in general. What’s the benefit in XmlWriter?

Here’s what it looks like. I’m building an OpenSearch XML doc based on the domain you come in from.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/xml";

    string domain = WebUtils.ReturnParsedSourceUrl(null); //returns something like www.sample.com
    string cachedChan = context.Cache[domain + "_opensearchdescription"] as String;

    if (cachedChan == null)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        sb.Append("<OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">");
        sb.Append("    <ShortName>Search</ShortName>");
        sb.Append("    <Description>Use " + domain + " to search.</Description>");
        sb.Append("    <Contact>contact@sample.com</Contact>");
        sb.Append("    <Url type=\"text/html\" method=\"get\" template=\"http://" + domain + "/Search.aspx?q={searchTerms}\" />");
        sb.Append("    <moz:SearchForm>http://" + domain + "/Search.aspx</moz:SearchForm>");
        sb.Append("    <Image height=\"16\" width=\"16\" type=\"image/x-icon\">http://" + domain + "/favicon.ico</Image>");
        sb.Append("</OpenSearchDescription>");

        cachedChan = sb.ToString();

        context.Cache.Insert(domain + "_opensearchdescription", cachedChan, null, DateTime.Now.AddDays(14), TimeSpan.Zero);
    }

    context.Response.Write(cachedChan);
}

Followup, ~2 years later
I realized that what I meant to say, and completely failed to say it is: what is the benefit of gobs of code using XML classes to generate this file, vs. just using strings? Is there one? Is this worse than (for example) John Saunder’s example?

I used Jim Schubert’s method, opting for ‘I can read this and it makes sense’ rather than vying for ‘correctness’. I’m glad I did. There’s nothing wrong with John Saunder’s example- but I felt that it was way overbearing for what I was trying to accomplish. Pragmatism? Maybe.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:27:03+00:00Added an answer on May 14, 2026 at 6:27 am

    That’s very wrong. Use one of the .NET APIs which understand XML to write XML.

    Using a System.Xml.XmlWriter will not cause any performance problem by loading “any extra libraries”.


    The reason to use the XML APIs is that they understand the rules of XML. For instance, they’ll know the set of characters that need to be quoted inside an element, and the different set that need to be quoted inside an attribute.

    This might not be an issue in your case: maybe you’re certain that domain will not have any characters in it that will need to be quoted. In any broader situation, it’s best to let the XML APIs do XML – which they know how to do – so you don’t have to do it yourself.


    Here’s an example of how easy it is to produce valid XML using LINQ to XML:

    public static string MakeXml()
    {
        XNamespace xmlns = "http://a9.com/-/spec/opensearch/1.1/";
        XNamespace moz = "http://www.mozilla.org/2006/browser/search/";
        string domain = "http://localhost";
        string searchTerms = "abc";
        var doc = new XDocument(
            new XDeclaration("1.0", "UTF-8", "yes"),
            new XElement(
                xmlns + "OpenSearchDescription",
                new XElement(xmlns + "ShortName", "Search"),
                new XElement(
                    xmlns + "Description",
                    String.Format("Use {0} to search.", domain)),
                new XElement(xmlns + "Contact", "contact@sample.com"),
                new XElement(
                    xmlns + "Url",
                    new XAttribute("type", "text/html"),
                    new XAttribute("method", "get"),
                    new XAttribute(
                        "template",
                        String.Format(
                            "http://{0}/Search.aspx?q={1}",
                            domain,
                            searchTerms))),
                new XElement(
                    moz + "SearchForm",
                    String.Format("http://{0}/Search.aspx", domain)),
                new XElement(
                    xmlns + "Image",
                    new XAttribute("height", 16),
                    new XAttribute("width", 16),
                    new XAttribute("type", "image/x-icon"),
                    String.Format("http://{0}/favicon.ico", domain))));
        return doc.ToString(); // If you _must_ have a string
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've notice an issue - it feels like a bug but I suspect a
I'm completely new to AIR but what I'm trying to do feels like it
Sometimes it feels that my company is the only company in the world using
Want to know what the stackoverflow community feels about the various free and non-free
I have a Website that is really slow and feels really bad when using
I'm aware of the Substance look and feels and that they have a Office
I have a relationship in a Core Data model that feels like it wants
Do languages become more verbose as they mature? It feels like each new version
Given the code: new Thread(new BackgroundWorker()).start(); Intuitively it feels like the BackgroundWorker instance should
MS is calling Azure an Operating System. To me, it feels much more like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.