Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AltChunk破坏富文本内容控制_C#_Ms Word_Vsto_Openxml - Fatal编程技术网

C# AltChunk破坏富文本内容控制

C# AltChunk破坏富文本内容控制,c#,ms-word,vsto,openxml,C#,Ms Word,Vsto,Openxml,我使用AltChunk对象将数据从docx文件复制到另一个文件中的富文本内容控件。这本书很好用。但是现在内容控件不能转换为OpenXml中的SdtElement,也不能转换为VSTO中的ContentControl 这是我使用的代码 SdtElement sdtElement = destinationdocument.MainDocumentPart.Document.Body.Descendants<SdtElement>().Where(b => b.SdtPropert

我使用
AltChunk
对象将数据从
docx
文件复制到另一个文件中的富文本内容控件。这本书很好用。但是现在内容控件不能转换为OpenXml中的
SdtElement
,也不能转换为VSTO中的
ContentControl

这是我使用的代码

SdtElement sdtElement = destinationdocument.MainDocumentPart.Document.Body.Descendants<SdtElement>().Where(b => b.SdtProperties.GetFirstChild<Tag>() != null).FirstOrDefault();
string altChunkId = "AltChunkId" + Guid.NewGuid().ToString();
AlternativeFormatImportPart chunk = destinationdocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImport    PartType.WordprocessingML, altChunkId);
chunk.FeedData(File.Open("sourceFile", FileMode.OpenOrCreate));
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
sdtElement.RemoveAllChildren();
sdtElement.Append(altChunk);
SdtElement SdtElement=destinationdocument.MainDocumentPart.Document.Body.subjects()。其中(b=>b.SdtProperties.GetFirstChild()!=null)。FirstOrDefault();
字符串altChunkId=“altChunkId”+Guid.NewGuid().ToString();
AlternativeFormatImportPart chunk=destinationdocument.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImport PartType.WordprocessingML,altChunkId);
FeedData(File.Open(“sourceFile”,FileMode.OpenOrCreate));
AltChunk AltChunk=新的AltChunk();
altChunk.Id=altChunkId;
sdtElement.RemoveAllChildren();
附加(altChunk);
第一次代码运行正常。但是在第二次运行时,第一行抛出一个无法强制转换的异常。在客户端使用VSTO时也会出现同样的问题,
ContentControl
对象无法保存插入了
AltChunk
的内容控件。不知何故,此过程破坏了富文本内容控件


我做错什么了吗?或者有更好的替代方法吗?

wordDocument.MainDocumentPart.Document.Body.subjects()返回
IEnumerable
并将其分配给
SDTeletNT
。尝试使用
var
或实际返回类型

更新:

你的代码是有效的。你做错的是这一行
sdtElement.removeallchildrent()

sdt元素(内容控件)包含其他元素,如sdtPr(内容控件属性)、sdtContent(内容控件内的实际内容)等,如下图所示

 <w:sdt>
    <w:sdtPr>
        ...
    </w:sdtPr>
    <w:sdtContent>
      ....
    </w:sdtContent>
  </w:sdt>
这将使您的程序在第二次运行时引发异常,作为第
destinationdocument.main documentpart.Document.Body.subjections()行运行,其中(b=>b.SdtProperties.GetFirstChild()!=null)
替换的文档sdt元素没有
sdt属性
,也没有
标记
sdt内容

要解决此问题,请尝试将altchunk块直接插入内容控件内容元素(
sdtContent
),而不是sdt元素,如下所示:

using (
    FileStream fileStream = File.Open("file.docx",
                                        FileMode.Open))
{
    chunk.FeedData(fileStream);
    AltChunk altChunk = new AltChunk();
    altChunk.Id = altChunkId;
    //sdtElement.RemoveAllChildren();
    sdtElement.Elements<SdtContentBlock>().FirstOrDefault().Append(altChunk); // This is going to add to the existing content.
}
使用(
FileStream FileStream=File.Open(“File.docx”,
FileMode.Open)
{
FeedData(fileStream);
AltChunk AltChunk=新的AltChunk();
altChunk.Id=altChunkId;
//sdtElement.RemoveAllChildren();
sdtElement.Elements().FirstOrDefault().Append(altChunk);//这将添加到现有内容中。
}

希望这有帮助

如果您在问题中包含异常详细信息,将有助于获得更集中的答案。它有LINQ扩展方法,可以为我转换它。它以前工作得很好。问题也发生在VSTO中。问题已经确定。内容控件不知何故已损坏。您的代码未显示sdtElement是什么以及您在文档中插入altchunk的位置。请更新您的问题,以包括这些详细信息。
using (
    FileStream fileStream = File.Open("file.docx",
                                        FileMode.Open))
{
    chunk.FeedData(fileStream);
    AltChunk altChunk = new AltChunk();
    altChunk.Id = altChunkId;
    //sdtElement.RemoveAllChildren();
    sdtElement.Elements<SdtContentBlock>().FirstOrDefault().Append(altChunk); // This is going to add to the existing content.
}