如何防止xml转换为


Aln*_*dru 11 c# xml ms-word

我这里有一个小代码:

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();
Run Code Online (Sandbox Code Playgroud)

我有一个问题,基本上/ r/n或新行在
属性中转换,但我不想拥有它,我想保留它/ r/n就像我使用这个XML与Microsoft Word文档模板,新的行没有实现,虽然它是多行文字,在word文档中我只得到空格.但没有新线:/

任何人有任何想法?

虽然我在模板中设置了允许多行int属性字段.

var*_*bas 12

实际上你得到的行为与之
相同Environment.NewLine.你可以做一个简单的测试来确认这一点(TextBoxes在你FormMultiline property设置中添加两个True:textBox1textBox2):

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

string text = element.ToString().Replace("
", Environment.NewLine);
textBox2.Text = text; ///r/n
Run Code Online (Sandbox Code Playgroud)

另一方面,如果你想要避免该
部分(例如:因为想要将给定的字符串输出到不在.NET上工作的外部程序),你可以Replace在处理XElement和新行之后依赖上述内容.