none
Delete a content control into a docx file RRS feed

  • Question

  • Hi,

    I want to modify a docx file. In this docx file, I have a content control and i want to take the content of the content control and, inserting it after the content control block before supress it.

    But i don't want to insert the "<w:sdtContent>" in my docx file in order to have a valid docx. So I want to know if I can write into the document.xml file using a string?

     

          foreach (SdtBlock sdt in sdtList)
          {
    
            OpenXmlElement sdtc = sdt.GetFirstChild<SdtContentBlock>();
            OpenXmlElement parent = sdt.Parent;
            parent.Append(sdtc.Clone());
          }
    With this code I have an invalid docx because I have a sdtContent in the xml wich is not in a sdt.

    Thank you for your help!

    Thursday, June 3, 2010 10:05 AM

Answers

  • I find an other solution.

    MainDocumentPart mainPart = _wordDocument.MainDocumentPart;
          List<SdtBlock> sdtList = mainPart.Document.Descendants<SdtBlock>().ToList();
          foreach (SdtBlock sdt in sdtList)
          {
    
            OpenXmlElement sdtc = sdt.GetFirstChild<SdtContentBlock>();
            OpenXmlElement parent = sdt.Parent;
    
            OpenXmlElementList elements = sdtc.ChildElements;
            
            var mySdtc = new SdtContentBlock(sdtc.OuterXml);
             
            foreach (OpenXmlElement elem in elements)
            {
               parent.Append((OpenXmlElement)elem.Clone());                   
            }
            
            sdt.Remove();
          }
    
          mainPart.Document.Save();
          Close();
    thank you for your help Jan !

    • Marked as answer by AminB Monday, June 7, 2010 9:00 AM
    Monday, June 7, 2010 9:00 AM

All replies

  • Hi,

    probably you should use a second loop over all child elements of the SdtContent. Insert them actually before the ContentControl to keep the correct order.

    foreach (SdtBlock sdt in sdtList)
    {
      OpenXmlElement sdtc = sdt.GetFirstChild<SdtContentBlock>();
      OpenXmlElement parent = sdt.Parent;
      foreach(OpenXmlElement child in sdtc.ChildElements)
      {
        parent.InsertBeforeSelf(child.Clone());
      }
    }
    

    Hope this helps,

    -Jan

    Friday, June 4, 2010 7:49 AM
  • I find an other solution.

    MainDocumentPart mainPart = _wordDocument.MainDocumentPart;
          List<SdtBlock> sdtList = mainPart.Document.Descendants<SdtBlock>().ToList();
          foreach (SdtBlock sdt in sdtList)
          {
    
            OpenXmlElement sdtc = sdt.GetFirstChild<SdtContentBlock>();
            OpenXmlElement parent = sdt.Parent;
    
            OpenXmlElementList elements = sdtc.ChildElements;
            
            var mySdtc = new SdtContentBlock(sdtc.OuterXml);
             
            foreach (OpenXmlElement elem in elements)
            {
               parent.Append((OpenXmlElement)elem.Clone());                   
            }
            
            sdt.Remove();
          }
    
          mainPart.Document.Save();
          Close();
    thank you for your help Jan !

    • Marked as answer by AminB Monday, June 7, 2010 9:00 AM
    Monday, June 7, 2010 9:00 AM