后代在Word文档中获取零个元素 [英] Descendants<T> gets zero elements in Word doc
问题描述
我在更新Word文档中的超链接时遇到问题(Q 如何在Word文档中更新正文和超链接),并且正在放大Descendants<T>()
调用,但无法正常工作.这是我的代码:
I am having trouble updating a Hyperlink in a Word doc (Q How to update the body and a hyperlink in a Word doc ) and am zooming in on the Descendants<T>()
call not working. Here is my code:
using DocumentFormat.OpenXml.Packaging; //from NuGet ClosedXML
using DocumentFormat.OpenXml.Wordprocessing; //from NuGet ClosedXML
WordprocessingDocument doc = WordprocessingDocument.Open(...filename..., true);
MainDocumentPart mainPart = doc.MainDocumentPart;
IEnumerable<Hyperlink> hLinks = mainPart.Document.Body.Descendants<Hyperlink>();
因为mainPart
获得了一个值,所以文档被打开,确定.但是hLinks
没有任何元素.如果我在Word中打开Word文档,则会显示一个超链接并且可以正常工作.
The doc is opened OK because mainPart
gets a value. But hLinks
has no elements. If I open the Word doc in Word, a hyperlink is present and working.
在立即窗口中,我看到以下值:
In the Immediate Window I see the following values:
mainPart.Document.Body
-->
{DocumentFormat.OpenXml.Wordprocessing.Body}
ChildElements: {DocumentFormat.OpenXml.OpenXmlChildElements}
ExtendedAttributes: {DocumentFormat.OpenXml.EmptyEnumerable<DocumentFormat.OpenXml.OpenXmlAttribute>}
FirstChild: {DocumentFormat.OpenXml.OpenXmlUnknownElement}
HasAttributes: false
HasChildren: true
InnerText: "
lots of data, e.g:
...<w:t>100</w:t>...
mainPart.Document.Body.Descendants<Text>().First()
-->
Exception: "Sequence contains no elements"
如果我什至找不到文本部分,应该如何查找和替换超链接?
If I cannot even find the text parts, how should I ever find and replace the hyperlink?
推荐答案
我的Word文档似乎有问题;它是用工具生成的.使用用Word创建的另一个Word文档进行测试可获得更好的结果.我正在努力...
Something seems to be wrong with my Word doc; it was generated with a tool. Testing with another Word doc, created with Word, gives better results. I am working on it ...
使用普通的Word文档,查看
With a regular Word doc, looking at
doc.MainDocumentPart.Document.Body.InnerXml
值开头:
<w:p w:rsidR=\"00455325\" w:rsidRDefault=\"00341915\"
xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">
<w:r>
<w:t>Hello World!
但使用我正在测试的doc一词,它来自我自己制作的工具:
but with the word doc I am testing with, which comes from a tool I myself made:
<w:body xmlns:w=\"http://schemas.openxmlforma...
这解释了很多. 我将不得不修复我的工具:-)
This explains a lot. I will have to fix my tool :-)
更新:
解决方法是,这没有提供要插入Word Doc的数据的正确部分:
The fix was that this did not give the correct part of data to insert in the Word Doc:
string strDocumentXml = newWordContent.DocumentElement.InnerXml;
但这是正确的数据:
string strDocumentXml = newWordContent.DocumentElement.FirstChild.OuterXml;
使用以下调试器进行检查:
Inspection with the debugger of:
doc.MainDocumentPart.Document.Body.InnerXml
如上所述,
确认.现在,后代"调用将返回预期的数据,并且更新超链接有效.
as mentioned above, confirmed it. The Descendants call now returns the expected data, and updating the hyperlink works.
旁注:
我显然已经修复了我的应用程序中的一个错误,但是除了更新超链接之外,该应用程序之前还可以正常运行,并且存在该错误:-)
I clearly fixed a bug in my app, but, apart from updating the hyperlink, the app worked perfectly OK before, with that bug :-)
这篇关于后代在Word文档中获取零个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!