What's So Bad About Building Xml with String Concatenation

What's so bad about building XML with string concatenation?

You can end up with invalid XML, but you will not find out until you parse it again - and then it is too late. I learned this the hard way.

What's so bad about building XML with string concatenation?

You can end up with invalid XML, but you will not find out until you parse it again - and then it is too late. I learned this the hard way.

Strings concatenation in XML (build.xml)

You can just do:

<echo message="${fname1}${fname2}"/> 

or

<echo>${fname1}${fname2}</echo>

Much more information on how properties work in ANT, and particularly how property expansion works can be found in the concepts section in the ant manual: http://ant.apache.org/manual/properties.html

String Concatenation unsafe in C#, need to use StringBuilder?

Apart from what you're doing is probably best done with XML APIs instead of strings or StringBuilder I doubt that the error you see is due to string concatenation. Maybe switching to StringBuilder just masked the error or went over it gracefully, but I doubt using strings really was the cause.

Built-in string formatting vs string concatenation as logging parameter

I believe you have your answer there.

Concatenation is calculated beforehand the condition check. So if you call your logging framework 10K times conditionally and all of them evaluates to false, you will be concatenating 10K times with no reason.

Also check this topic. And check Icaro's answer's comments.

Take a look to StringBuilder too.

Concatenate multiple strings in XML?

No I don't think you can concatenate.

<string name="aaa">aaa</string>
<string name="bbb">bbb @string/aaa</string>

Output - bbb @string/aaa

If you do,

<string name="aaa">aaa</string>
<string name="bbb">@string/aaa bbb</string> -> This won't work it
will give compilation error

Because here it will search for a String with reference @string/aaa bbb which does not exists.

Problem in your case was, you where using @strings/aaa which should be @string/aaa

Concatenate Strings in the strings.xml file for Android

I am afraid that is not possible in strings.xml.

What you can do is create the final string programmatically using getString:

String outStr = getString(R.string.your_android_says) + 
" " + getString(R.string.hello);

Is using a StringBuilder for writing XML ok?

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
}


Related Topics



Leave a reply



Submit