Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's so bad about building XML with string concatenation?
    primarykey
    data
    text
    <p>In the thread <a href="https://stackoverflow.com/questions/423823/whats-your-favorite-programmer-ignorance-pet-peeve/424058#424058">What’s your favorite “programmer ignorance” pet peeve?</a>, the following answer appears, with a large amount of upvotes:</p> <p><code>Programmers who build XML using string concatenation.</code></p> <p>My question is, why is building XML via string concatenation (such as a <code>StringBuilder</code> in C#) bad?</p> <p>I've done this several times in the past, as it's sometimes the quickest way for me to get from point A to point B when to comes to the data structures/objects I'm working with. So far, I have come up with a few reasons why this isn't the greatest approach, but is there something I'm overlooking? Why should this be avoided?</p> <ol> <li>Probably the biggest reason I can think of is you need to escape your strings manually, and most new programmers (and even some experienced programmers) will forget this. It will work great for them when they test it, but then "randomly" their apps will fail when someone throws an &amp; symbol in their input somewhere. Ok, I'll buy this, but it's really easy to prevent the problem (<a href="http://msdn.microsoft.com/en-us/library/system.security.securityelement.escape.aspx" rel="nofollow noreferrer">SecurityElement.Escape</a> to name one).</li> <li>When I do this, I usually omit the XML declaration (i.e. <code>&lt;?xml version="1.0"?&gt;</code>). Is this harmful? </li> <li>Performance penalties? If you stick with proper string concatenation (i.e. <code>StringBuilder</code>), is this anything to be concerned about? Presumably, a class like <code>XmlWriter</code> will also need to do a bit of string manipulation...</li> <li>There are more elegant ways of generating XML, such as using <code>XmlSerializer</code> to automatically serialize/deserialize your classes. Ok sure, I agree. C# has a ton of useful classes for this, but sometimes I don't want to make a class for something really quick, like writing out a log file or something. Is this just me being lazy? If I am doing something "real" this is my preferred approach for dealing w/ XML.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload