C#OpenXML如何用Break()替换\r\n?

C#OpenXML如何用Break()替换\r\n?,c#,asp.net,.net,openxml,export-to-word,C#,Asp.net,.net,Openxml,Export To Word,我的数据库中有一个文本字段,它有一个多行的文本。 使用OpenXML和书签生成MS Word文档时,文本变成一行 我注意到,在每一行中,书签值都显示字符“\r\n”。 在寻找解决方案时,我找到了一些帮助我的答案,但我仍然有一个问题 我使用了run.Append(new Break());解决方案,但替换的文本也显示书签的名称 例如: bookmark test=“第一段中的大文本\r\n第二段” 如MS Word文档所示: 测试第一段中的大文本 第二段 谁能帮我去掉书签名吗 这是我的密码: pu

我的数据库中有一个文本字段,它有一个多行的文本。 使用OpenXML和书签生成MS Word文档时,文本变成一行

我注意到,在每一行中,书签值都显示字符“\r\n”。 在寻找解决方案时,我找到了一些帮助我的答案,但我仍然有一个问题

我使用了run.Append(new Break());解决方案,但替换的文本也显示书签的名称

例如: bookmark test=“第一段中的大文本\r\n第二段”

如MS Word文档所示:

测试第一段中的大文本

第二段

谁能帮我去掉书签名吗

这是我的密码:

public void UpdateBookmarksVistoria(string originalPath, string copyPath, string fileType)
    {
        string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

        // Make a copy of the template file.
        File.Copy(originalPath, copyPath, true);

        //Open the document as an Open XML package and extract the main document part.
        using (WordprocessingDocument wordPackage = WordprocessingDocument.Open(copyPath, true))
        {
            MainDocumentPart part = wordPackage.MainDocumentPart;
            //Setup the namespace manager so you can perform XPath queries 
            //to search for bookmarks in the part.
            NameTable nt = new NameTable();
            XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
            nsManager.AddNamespace("w", wordmlNamespace);
            //Load the part's XML into an XmlDocument instance.
            XmlDocument xmlDoc = new XmlDocument(nt);
            xmlDoc.Load(part.GetStream());

            //pega a url para exibir as fotos
            string url = HttpContext.Current.Request.Url.ToString();
            string enderecoURL;
            if (url.Contains("localhost"))
                enderecoURL = url.Substring(0, 26);
            else if (url.Contains("www."))
                enderecoURL = url.Substring(0, 24);
            else
                enderecoURL = url.Substring(0, 20);

            //Iterate through the bookmarks.
            int cont = 56;
            foreach (KeyValuePair<string, string> bookmark in bookmarks)
            {
                var res = from bm in part.Document.Body.Descendants<BookmarkStart>()
                          where bm.Name == bookmark.Key
                          select bm;
                var bk = res.SingleOrDefault();
                if (bk != null)
                {
                        Run bookmarkText = bk.NextSibling<Run>();
                        if (bookmarkText != null) // if the bookmark has text replace it
                        {
                            var texts = bookmark.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

                            for (int i = 0; i < texts.Length; i++)
                            {
                                if (i > 0)
                                    bookmarkText.Append(new Break());

                                Text text = new Text();
                                text.Text = texts[i];
                                bookmarkText.Append(text); //HERE IS MY PROBLEM
                            }
                        }
                        else // otherwise append new text immediately after it
                        {
                            var parent = bk.Parent; // bookmark's parent element
                            Text text = new Text(bookmark.Value);
                            Run run = new Run(new RunProperties());
                            run.Append(text);
                            // insert after bookmark parent
                            parent.Append(run);
                        }

                        bk.Remove(); // we don't want the bookmark anymore
                }
            }

            //Write the changes back to the document part.
            xmlDoc.Save(wordPackage.MainDocumentPart.GetStream(FileMode.Create));
            wordPackage.Close();
        }}
public void updatebook marksvistoria(字符串原始路径、字符串复制路径、字符串文件类型)
{
字符串wordmlNamespace=”http://schemas.openxmlformats.org/wordprocessingml/2006/main";
//制作模板文件的副本。
Copy(originalPath,copyPath,true);
//将文档作为打开的XML包打开,并提取主文档部分。
使用(WordprocessingDocument wordPackage=WordprocessingDocument.Open(copyPath,true))
{
MainDocumentPart=wordPackage.MainDocumentPart;
//设置名称空间管理器,以便可以执行XPath查询
//搜索零件中的书签。
NameTable nt=新的NameTable();
XmlNamespaceManager nsManager=新的XmlNamespaceManager(nt);
nsManager.AddNamespace(“w”,wordmlNamespace);
//将部件的XML加载到XmlDocument实例中。
XmlDocument xmlDoc=新的XmlDocument(nt);
Load(part.GetStream());
//把一个url改为fotos
字符串url=HttpContext.Current.Request.url.ToString();
字符串结束符;
if(url.Contains(“localhost”))
enderecurl=url.Substring(0,26);
else if(url.Contains(“www.”)
enderecurl=url.Substring(0,24);
其他的
enderecurl=url.Substring(0,20);
//反复浏览书签。
int cont=56;
foreach(书签中的KeyValuePair书签)
{
var res=来自part.Document.Body.subjects()中的bm
其中bm.Name==bookmark.Key
选择bm;
var bk=res.SingleOrDefault();
如果(bk!=null)
{
运行bookmarkText=bk.NextSibling();
if(bookmarkText!=null)//如果书签有文本,则替换它
{
var text=bookmark.Value.Split(new[]{Environment.NewLine},StringSplitOptions.None);
for(int i=0;i0)
bookmarkText.Append(newbreak());
Text Text=新文本();
text.text=文本[i];
bookmarkText.Append(text);//这是我的问题
}
}
else//else在新文本之后立即追加新文本
{
var parent=bk.parent;//书签的父元素
Text Text=新文本(bookmark.Value);
运行=新运行(新运行属性());
run.Append(文本);
//在书签父项后插入
parent.Append(运行);
}
bk.Remove();//我们不再需要书签了
}
}
//将更改写回文档部分。
Save(wordPackage.MainDocumentPart.GetStream(FileMode.Create));
Close();
}}

在C中,这些是转义字符\r是回车符,\n是换行符(换行符)。谢谢@Kevin,但我的问题是,在生成Word文档时,书签名称与值一起出现。我尝试过使用bookmarkText.GetFirstChild.Text=Text.Text,但在本例中,当我有新行时,文档中只生成了最后一行。在添加
文本之前,您是否尝试过
bookmarkText
中的
移除所有儿童()
?在C中,这些是转义字符\r是回车符,\n是换行符(换行符)。谢谢@Kevin,但我的问题是,在生成Word文档时,书签名称与值一起出现。我尝试过使用bookmarkText.GetFirstChild.Text=Text.Text,但在这种情况下,当我有新行时,文档中只生成了最后一行。在添加
文本之前,您是否尝试过
bookmarkText
删除所有儿童()?