Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

c# - Cannot insert the OpenXmlElement "newChild" because it is part of a tree

The Title states the error I am getting. I'm trying to hide all the text in a word doc using OpenXml. Currently when I try and append the Paragraph properties I receive the above error. I can't find much about this error online.

Code that returns error

        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;

            }
       }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Normally this error can be fixed by Cloning whatever node is causing the exception and then inserting that cloned value. Something like this:

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);

This will create one Color instance and then use the same instance for all the borders by cloning the original instance then inserting it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...