gpt4 book ai didi

c# - 无法插入 OpenXmlElement "newChild",因为它是树的一部分

转载 作者:可可西里 更新时间:2023-11-01 08:01:54 24 4
gpt4 key购买 nike

标题说明了我遇到的错误。我正在尝试使用 OpenXml 隐藏 word 文档中的所有文本。目前,当我尝试附加 Paragraph 属性时,我收到上述错误。我在网上找不到太多关于此错误的信息。

返回错误的代码

        using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(mDoc_copy, true))
{
// Manage namespaces to perform XPath queries.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);

// Get the document part from the package.
// Load the XML in the document part into an XmlDocument instance.
XmlDocument xdoc = new XmlDocument(nt);
xdoc.Load(wdDoc.MainDocumentPart.GetStream());

MainDocumentPart main = wdDoc.MainDocumentPart;

IEnumerable<OpenXmlElement> elem = main.Document.Body.Descendants().ToList();
Paragraph p;
ParagraphProperties pp = new ParagraphProperties();
ParagraphMarkRunProperties prmp = new ParagraphMarkRunProperties();
Vanish v = new Vanish();
apprmp.AppendChild<Vanish>(v);
pp.AppendChild<ParagraphMarkRunProperties>(apprmp);


foreach (Paragraph para in main.Document.Body.Descendants<Paragraph>().ToList())
{


para.ParagraphProperties = pp;

}
}

最佳答案

通常可以通过克隆任何导致异常的节点然后插入该克隆值来修复此错误。像这样:

LeftBorder leftBorder = new LeftBorder() { Style = BorderStyleValues.Thin };
TopBorder topBorder = new TopBorder() { Style = BorderStyleValues.Thin };
RightBorder rightBorder = new RightBorder() { Style = BorderStyleValues.Thin };
BottomBorder bottomBorder = new BottomBorder() { Style = BorderStyleValues.Thin };

Color color = new Color() { Auto = true, Rgb = rgbHexValue == string.Empty ? new HexBinaryValue("00000000") : new HexBinaryValue(rgbHexValue) };

leftBorder.Color = color;
topBorder.Color = (Color)color.CloneNode(true);
rightBorder.Color = (Color)color.CloneNode(true);
bottomBorder.Color = (Color)color.CloneNode(true);

这将创建一个 Color 实例,然后通过克隆原始实例然后插入它来为所有边框使用相同的实例。

关于c# - 无法插入 OpenXmlElement "newChild",因为它是树的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16320537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com