Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep xml from converting /r/n into 


Tags:

c#

ms-word

xml

I have here a small code:

string attributeValue = "Hello" + Environment.NewLine + " Hello 2";

XElement element = new XElement("test");
XElement subElement = new XElement("subTest");
XAttribute attribute = new XAttribute("key", "Hello");
XAttribute attribute2 = new XAttribute("key2", attributeValue);
subElement.Add(attribute);
subElement.Add(attribute2);
element.Add(subElement);

Console.Write(element.ToString());
Console.ReadLine();

I have an issue, basically the /r/n or the new line is converted in 
 in attribute, but I dont want to have it, I want to keep it /r/n as when I use this XML with the Microsoft Word documents template, the new lines are not implemented, although it is multilined text, in word document I only get the spaces. But no new lines :/

Anyone has any idea?

Although i've set the allow multi line int he property of the field in the template.

like image 386
Alnedru Avatar asked Sep 09 '13 07:09

Alnedru


1 Answers

Actually the behaviour you get with 
 is the same than the one of Environment.NewLine. You can do a simple test to confirm this (add two TextBoxes to your Form with the Multiline property set to True: textBox1 and textBox2):

textBox1.Text = element.ToString(); //

string text = element.ToString().Replace("
", Environment.NewLine);
textBox2.Text = text; ///r/n

On the other hand, if you want to avoid the 
 part anyway (for example: because of wanting to output the given string to an external program not working on .NET), you can just rely on the aforementioned Replace after dealing with XElement and new lines.

like image 71
varocarbas Avatar answered Sep 17 '22 17:09

varocarbas