无法插入OpenXmlElement" newChild对象"因为它是树的一部分 [英] Cannot insert the OpenXmlElement "newChild" because it is part of a tree

查看:1210
本文介绍了无法插入OpenXmlElement" newChild对象"因为它是树的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题指出我得到的错误。我想为隐藏使用OpenXML中的Word文档中的所有文本。目前,当我尝试追加段落属性我收到上面的错误。我找不到太多关于此错误的联机。



这将返回错误代码

 使用(Wordpro​​cessingDocument wdDoc = Wordpro​​cessingDocument.Open(mDoc_copy,真))
{
//管理命名空间来执行XPath查询。
NameTable新台币=新NameTable();
的XmlNamespaceManager nsManager =新的XmlNamespaceManager(NT);
nsManager.AddNamespace(W,wordmlNamespace);

//获取文档部件从包装。
//加载文档部分XML到XmlDocument实例。
XDOC的XmlDocument =新的XmlDocument(NT);
xdoc.Load(wdDoc.MainDocumentPart.GetStream());

MainDocumentPart主要= wdDoc.MainDocumentPart;

IEnumerable的< OpenXmlElement> 。ELEM = main.Document.Body.Descendants()了ToList();
段磷;
ParagraphProperties PP =新ParagraphProperties();
ParagraphMarkRunProperties prmp =新ParagraphMarkRunProperties();
已逝V =新已逝();
apprmp.AppendChild<&消失GT;(五);
pp.AppendChild< ParagraphMarkRunProperties>(apprmp);


的foreach(在main.Document.Body.Descendants< para段落,段落方式>()了ToList())
{


para.ParagraphProperties =页;

}
}


解决方案

通常这个错误可以通过克隆的任何节点导致了异常,然后插入该克隆的价值是固定的。事情是这样的:

  LeftBorder leftBorder =新LeftBorder(){类型= BorderStyleValues​​.Thin}; 
TopBorder topBorder =新TopBorder(){类型= BorderStyleValues​​.Thin};
RightBorder rightBorder =新RightBorder(){类型= BorderStyleValues​​.Thin};
BottomBorder bottomBorder =新BottomBorder(){类型= BorderStyleValues​​.Thin};

的色色=新的色彩(){自动= TRUE,RGB = rgbHexValue ==的String.Empty?新HexBinaryValue(00000000):新HexBinaryValue(rgbHexValue)};

leftBorder.Color =颜色;
topBorder.Color =(彩色)color.CloneNode(真);
rightBorder.Color =(彩色)color.CloneNode(真);
bottomBorder.Color =(彩色)color.CloneNode(真);

这将创建一个颜色实例,然后通过克隆原始实例,然后将其插入用于所有的边框相同的实例。


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;

            }
       }

解决方案

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.

这篇关于无法插入OpenXmlElement&QUOT; newChild对象&QUOT;因为它是树的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆