How to save xml from converting / r / n to & # xD; & # xA;

I have a little 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 a problem, basically, / r / n or a new line is converted to 
 in the attribute, but I don’t want to have it, I want to save it / r / n, as when using this XML with the Microsoft Word document template, new lines are not implemented, although this is multi-line text, I get spaces in the text document. But no new lines: /

Does anyone have any idea?

Although I set the property allow multi line int he in the field in the template.

+8
c # xml ms-word
source share
1 answer

Actually the behavior you get with 
 matches the behavior 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 still want to avoid the 
 (for example: because of the desire to output this line to an external program not running on .NET), you can simply rely on the aforementioned Replace after working with XElement and new lines.

+8
source share

All Articles