简体   繁体   中英

Paragraphs and table not found in a Word Document (open XML)

I have created simple open XML document (.dotx) using MS Word. The file contains simple text and one table. I am trying to replace few custom placeholders in the text with the new text, however the below snippet cannot find any Paragraph nor Table in the document. I have tried to create several new.dotx files and also tried different variations of the document type ie .dotx and (Strict Open XML).docx using MS Word but the issue still remains.

   using (WordprocessingDocument doc =
            WordprocessingDocument.Open(templatePath, true))
        {

            var body = doc.MainDocumentPart.Document.Body;
            var paras = body.Elements<Paragraph>(); // <-- always empty
            var tables = body.Descendants<Table>(); // <-- always empty

            foreach (Table t in tables)
            {
                t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
            }

            foreach (var para in paras)
            {
                foreach (var run in para.Elements<Run>())
                {
                    foreach (var text in run.Elements<Text>())
                    {
                        if (text.Text.Contains("###name###"))
                        {
                            text.Text = text.Text.Replace("###name###", "Sample");
                        }
                    }
                }
            }
            doc.SaveAs(resultPath);
        }

Funny enough if I use the below snippet from MS docs it does work, however It is not clear how to add additional rows to the table. Therefore, I would prefer to use the first method istead. Any idea what could be the issue with the file or the above code?

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templatePath, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }

            Regex regexText = new Regex("###name###");
            docText = regexText.Replace(docText, "My Text!");

            using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
        }

When you create your document with Editor like MS Word it could add some containers, which wrap your paragraph, I'd suggest you check xml which generated. To do that, you can simply rename .docx to .zip and open that archive.

Inside you will found files like that在此处输入图像描述

You will need to open word/document.xml with any text editor and see, if <w:p> there and it is direct child of <w:body> . If it is not direct, use descendants method.

var paras = body.Descendants<Paragraph>(); // <-- always empty

Elements finds only direct children.

Descendants finds children at any level.

Also, most common issue is wrong namespace, as Paragraph exists in the amny of namespaces of OpenXml , you have to use using DocumentFormat.OpenXml.Wordprocessing;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM