openXmlSdk在Run Element中插入新行

我在Run Element中有文本。 我试图用line break替换字符串中的\r ..

案文如下

 This is an example project for testing purposes. \rThis is all sample data, none of this is real information. \r\rThis field allows for the entry of more information, a larger text field for example purposes 

并且Run Element的innerXml被翻译成

     This is an example project for testing purposes. This is all sample data, none of this is real information.<w:br /><w:br />This field allows for the entry of more information, a larger text field for example purposes. 

生成文档时未插入换行符。

如何用换行符替换 每个’\ r’?

我试过了。

 s.InnerXml = s.InnerXml.Replace("<w:br />", ""); 

我也尝试直接在字符串中替换它,但这也不起作用。

它只是一个字符串

 This is an example project for testing purposes. This is all sample data, none of this is real information.This field allows for the entry of more information, a larger text field for example purposes. 

文档声明Text元素包含文字文本。 SDK不会对您写入Text元素的字符串中的换行符进行假设。 如果你想要一个rest或者你想要一个段落,它会怎么知道?

如果要从文字字符串构建文档,则需要完成一些工作:

 using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; namespace OXmlTest { class Program { static void Main(string[] args) { using (var wordDocument = WordprocessingDocument .Create("c:\\deleteme\\testdoc.docx", WordprocessingDocumentType.Document)) { MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph p = body.AppendChild(new Paragraph()); Run r = p.AppendChild(new Run()); string theString = "This is an example project for testing purposes. \rThis is all sample data, none of this is real information. \r\rThis field allows for the entry of more information, a larger text field for example purposes"; foreach (string s in theString.Split(new char[] { '\r' })) { r.AppendChild(new Text(s)); r.AppendChild(new Break()); } wordDocument.Save(); } } } } 

最终文件

      This is an example project for testing purposes.   This is all sample data, none of this is real information.     This field allows for the entry of more information, a larger text field for example purposes