newline when serializing textbox

I'm trying to serialize the contents of a textbox to xml. The serialization works fine, but when i deserialize the contents and try to fill the textbox.text, new lines show up as litte squares instead of a new line. How would i solve this problem

I guess one way would be to split the string into an array and filling the textbox this way, but i'm not quite sure how to do this either.


Answer this question

newline when serializing textbox

  • Scott Munro

    It sounds like your \r\n's may be getting converted to \n's. Assuming your textbox IS multi-line I would check the return value of .IndexOf("\r") and the .IndexOf("\n") methods and see what values they are returning. If one is returning -1 and one is returning a non negative number then this means that they are being replaced. In which case, depending upon what character it is you would simply say .Replace("\n", Environment.NewLine)
    (assuming that \n was being found and not \r) .


  • sqldummy

    Ok, as you said, newlines are serialized as "\n". I tried the <textbox>.Text.Replace("\n",Environment.Newline) but the textbox still shows the same little cute squares.

    It would be interesting to know why this doesn't work.

    Breaking the string up into an array and setting textbox.Lines worked fine though.

    Thanks alot! really helpful

  • Sara Hentzel

    Hi,

    when serializing text to Xml, line breaks are always serialized as line feed characters (\n). [see http://www.w3.org/TR/2004/REC-xml-20040204/#sec-common-syn].

    The other way you're asking would look something like:

    textBox1.Lines =  myDeserializedText.Split('\n');

    Andrej



  • newline when serializing textbox