foreach word items -- openxml

The above xml text is a super simplified version but the structure is exactly the same. A simpler version of the structure is here Document > Body > Table > Table_Row > Table_Cell > Paragraph > Run > Field_Char > Form_Field_Data > Checkbox

Below is my extremely ugly way of iterating through the xml text in an attempt to locate the bookmark and checkbox. Note: I was only planning on getting it working and then I will optimise later.

        foreach (var table in document.MainDocumentPart.Document.Body.ChildElements.Where(t => t is DocumentFormat.OpenXml.Wordprocessing.Table))
        {
            foreach (var row in table.ChildElements.Where(r => r is DocumentFormat.OpenXml.Wordprocessing.TableRow))
            {
                foreach (var cell in row.ChildElements.Where(c => c is DocumentFormat.OpenXml.Wordprocessing.TableCell))
                {
                    foreach (var paragraph in cell.ChildElements.Where(p => p is DocumentFormat.OpenXml.Wordprocessing.Paragraph))
                    {
                        foreach (var run in paragraph.ChildElements.Where(r => r is DocumentFormat.OpenXml.Wordprocessing.Run))
                        {
                            foreach (var fieldChar in run.ChildElements.Where(fc => fc is DocumentFormat.OpenXml.Wordprocessing.FieldChar))
                            {
                                foreach (var formFieldData in fieldChar.ChildElements)
                                {
                                    foreach (var child in formFieldData.ChildElements)
                                    {
                                        //Check bookmark name
                                        //If bookmark name matches, 
                                        //Set checkbox value to true
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
posted @ 2018-04-04 10:03  赤狐(zcm123)  阅读(158)  评论(0编辑  收藏  举报