I’m using XDocument to cache a list of files.
<file id="20" size="244318208">a file with an &ersand.txt</file>
In this example, I used XText, and let it automatically escape characters in the file name, such as the & with &
<file id="20" size="244318208"><![CDATA[a file with an &ersand.txt]]></file>
In this one, I used XCData to let me use a literal string rather than an escaped one, so it appears in the XML as it would in my application.
I’m wondering if either of them is better than the other under any certain conditions, or if it is just personal taste. Also, if it means anything, the file names may or may not contain illegal characters.
Both are essentially the same and there is no specific “best practice”.
Personally, I reserve
<![CDATA[]]>for large amounts of text that requires lots of escaping (say bits of code or HTML markup).In this specific case, I would rather escape the
&to&as in your first example.