如何将节内容从一个文档复制到另一文档的节

问题描述

我正在尝试将部分内容一个文档复制到另一文档的同一部分。 我可以使用标题从文档中获取该部分的内容,但是当将此xml内容替换为另一个文档部分时,该文档不会打开。任何链接或示例都将有助于完成此方案。我正在使用开放式XML sdk .net核心c#

//get all descendants with pstyle
            var elements = xProcessDoc.Descendants(w + "pStyle");
            //get all pstyle elements with heading 1 - section headers
            var existList = (from el in elements
                             where (string)el.Attribute(w + "val") == "heading1"
                             select el).ToArray();
            if (sectionNumber != null)
            {
                var sectNumber = Convert.ToInt16(sectionNumber);
                //extraction logic for section
                var start = existList[sectNumber - 1];
                var ptag = start.Parent.Parent;
                //Remove all elements before the ptag
                IEnumerable<XElement> ptagBeforeElements = ptag.ElementsBeforeSelf();
                xProcessDoc.Descendants().Where(x => ptagBeforeElements.Contains(x)).Remove();

                //Remove all elements after the section end
                var end = existList[sectNumber];
                var endtag = end.Parent.Parent;
                IEnumerable<XElement> endtagAfterElements = endtag.ElementsAfterSelf();
                xProcessDoc.Descendants().Where(x => endtagAfterElements.Contains(x)).Remove();

                // remove the endth element
                var endtagelements = endtag.Elements();
                xProcessDoc.Descendants().Where(x => endtagelements.Contains(x)).Remove();
                var childElements = xProcessDoc.Root.Elements();// xProcessDoc.Root.Element(w + "body").Elements();
                var sectionxml = new StringBuilder();
                foreach (var item in childElements)
                {
                    sectionxml.Append(item.ToString() + "\n");
                }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...