0

I'm trying to add the ASCII carriage return 
 to the end of a string in an XmlNode's InnerText like this:

XmlNode spanNode = doc.CreateElement("span");
spanNode.InnerText = "carriage return here: 
";
rootNode.AppendChild(spanNode);

But this ends up escaping the string, so that it appears in rootNode's OuterXml like this:

carriage return here: 

How do I add this text if I actually want the unescaped carriage return?

EDIT: if it makes a difference, this is for text that will be displayed in a PDF annotation. I'm changing the value of the RC key that defines the rich text--removing some existing <span> elements with this carriage return and adding new ones that have an identical carriage return.

Here's what I want to end up with:

<root>
 <span>carriage return here: &#13;</span>
</root>
sigil
  • 9,370
  • 40
  • 119
  • 199
  • Possible duplicate of [Best way to encode text data for XML](http://stackoverflow.com/questions/157646/best-way-to-encode-text-data-for-xml) – Xavier J May 25 '16 at 19:03
  • @codenoire, I think I might be asking the opposite of that question. I don't want to encode the escaped version of the value, I actually want it to render as a carriage return when it's displayed in a PDF annotation (see my edit). – sigil May 25 '16 at 19:23

3 Answers3

1

You want to include a CDATA section. Otherwise the parser will interpret carriage returns as unneeded whitespace. The "duplicate" link i referred to (in comments above) in alludes to this, but MS has more info. You don't need to encode the carriage returns as long as they're in the CDATA section.

https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection(v=vs.110).aspx

For reference's sake , I've modified Microsoft's example. Take note of the \r \n characters.

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a CData section.
    XmlCDataSection CData;
    CData = doc.CreateCDataSection("All\r\n Jane\r\n Austen novels 25% off starting 3/23!");    

    //Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    root.AppendChild(CData);  

    Console.WriteLine("Display the modified XML...");        
    doc.Save(Console.Out);
  }
}
Xavier J
  • 4,326
  • 1
  • 14
  • 25
  • Please see my answer using `InnerXml`; do you foresee any problem if I go with that approach? – sigil May 25 '16 at 19:33
  • Encoding any whitespace characters outside of a CDATA section is harder to read, and if you open the xml file in certain editors they won't save the whitespace back in the manner that you created it. But -- if it's working... go with it. – Xavier J May 25 '16 at 19:38
  • Hmm...my solution is rendering properly in the PDF, but the actual ` ` CR is missing from the XML. I'll give your CDATA approach a try. – sigil May 25 '16 at 19:49
  • This seems to be working ok for adding carriage returns to the text, although I have to use `\r\n` instead of ` ` like I had hoped. – sigil May 26 '16 at 04:10
0

You are not in HTML, but in C#. Just use the appropriate string character escape sequence. If you don't know the exact escape sequence letter, just use the corresponding ASCII code or Unicode escape instead, e.g. \x0D or \x000D. (13 decimal is 0D hexadecimal)

Wormbo
  • 4,978
  • 2
  • 21
  • 41
  • This is actually a problem of displaying the XML in HTML because it's for display in a PDF. Please see my edit for more info. – sigil May 25 '16 at 19:27
0

Solved this really easily by setting InnerXml instead of InnerText:

XmlNode spanNode = doc.CreateElement("span");
spanNode.InnerXml = "carriage return here: &#13;";
rootNode.AppendChild(spanNode);

EDIT: this does not actually create the desired text. Trying to find a different approach.

sigil
  • 9,370
  • 40
  • 119
  • 199