Descendants gets zero elements in Word doc

前端 未结 2 1571
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-25 09:29

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() cal

2条回答
  •  生来不讨喜
    2021-01-25 10:29

    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 ...

    With a regular Word doc, looking at

    doc.MainDocumentPart.Document.Body.InnerXml
    

    the value starts with:

    
      
        Hello World!
    

    but with the word doc I am testing with, which comes from a tool I myself made:

    This explains a lot. I will have to fix my tool :-)


    Update:

    The fix was that this did not give the correct part of data to insert in the Word Doc:

    string strDocumentXml = newWordContent.DocumentElement.InnerXml;
    

    but instead this is the correct data:

    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.


    Side note:

    I clearly fixed a bug in my app, but, apart from updating the hyperlink, the app worked perfectly OK before, with that bug :-)

提交回复
热议问题