[xml] Adding a new line/break tag in XML

I have been trying to add a new line/break to code within XML and have been unsuccessful.

I have tried so far:

<br />
<br> 



Here is a sample of the code I am working with. I included "
" to show where the break is located within the code.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
  <item>
     <summary>Tootsie roll tiramisu macaroon wafer carrot cake.       
               &#xA; Danish topping sugar plum tart bonbon caramels cake.
     </summary>
  </item>

This question is related to xml

The answer is


The easiest way to give a line break is to do the following :
1> Add the following in CSS - e{display:block}
2> Now wherever you want to give a line break, type -

<e></e>

You don't need anything fancy: the following contains a new line (two, actually):

<summary>Tootsie roll tiramisu macaroon wafer carrot cake.       
         Danish topping sugar plum tart bonbon caramels cake.
</summary>

The question is, why isn't this newline having the desired effect: and that's a question about what the recipient of the XML is actually doing with it. For example, if the recipient is translating it to HTML and the HTML is being displayed in the browser, then the newline will be converted to a space by the browser. You need to tell us something about the processing pipeline.


You are probably using Windows, so new line is CR + LF (carriage return + line feed). So solution would be:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
  <item>
     <summary>Tootsie roll tiramisu macaroon wafer carrot cake.&#13;&#10;Danish topping sugar plum tart bonbon caramels cake.
     </summary>
  </item>

For Linux there is only LF and for Mac OS only CR.

In question there showed Linux way.


Wanted to add my solution:

&lt; br/ &gt;

which is basically the same as was suggested above
In my case I had to use &lt for < and &gt for > Simply putting <br /> did not work.


(Using system.IO)

You can simply use \n for newline and \t in front of the string to indent it.

For example in c#:

public string theXML() {
    string xml = "";
    xml += "<Scene>\n";
    xml += "\t<Character>\n";


    xml += "\t</Character>\n";
    xml += "</Scene>\n";
    return xml;
}

This will result in the output: http://prntscr.com/96dfqc


This solution worked to me:

<summary>Tootsie roll tiramisu macaroon wafer carrot cake. &#xD;Danish topping sugar plum tart bonbon caramels cake.</summary>

You will have the text in two lines.

This worked to me using the XmlReader.Read method.


just simply press enter it make a break

<![CDATA[this is
my text.]]>

Had same issue when I had to develop a fixed length field format.

Usually we do not use line separator for binary files but For some reason our customer wished to add a line break as separator between records. They set

< record_delimiter value="\n"/ >

but this didn't work as records got two additional characters:
< record1 > \n < record2 > \n.... and so on.

Did following change and it just worked.

< record_delimiter value="\n"/> => < record_delimiter value="&#xA;"/ >

After unmarshaling Java interprets as new line character.


Without using CDATA, try

<xsl:value-of select="'&#xA;'" />

Note the double and single quotes.

That is particularly useful if you are not creating xml aka text. <xsl:output method="text" />


This can be addressed simple by CSS attribute:

XML: <label name="pageTac"> Hello how are you doing? Thank you I'm Good</label>

CSS

.pageText{
white-space:pre !important; // this wraps the xml text.}

HTML / XSL

<tr>
        <td class="pageText"><xsl:value-of select="$Dictionary/infolabels/label[@name='pageTac']" />
            </td></tr>

The Way to do Line Break in XML is to use &#xA;

It worked for me in Eclipse IDE , when I was designing my XML layout & was using TextView.


You probably need to put it in a CDATA block to preserve whitespace

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="dummy.xsl"?>   
   <item>      
      <summary>
         <![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake.                       
            Danish topping sugar plum tart bonbon caramels cake.]]>
      </summary>   
   </item> 

New Line XML

with XML

  1. Carriage return: &#xD;
  2. Line feed: &#xA;

or try like @dj_segfault proposed (see his answer) with CDATA;

 <![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake.                       
            Danish topping sugar plum tart bonbon caramels cake.]]>