none
How to remove a Section and the contents enclosed in that section

    Question

  • In Word 2003 , wx:sect was acting like a container for the section. So if i need to remove the content in that section , i simply remove the  wx:sect node in that section., I would need to apply same or similar concept in word 2010  Open xml also.

    in my application we have coded in such a way that if a condition fails we will delete the section, which is ultimately we remove the <wx:sect> node.

    But now since we have no w:sect node, though we remove the w:sectpr it is just removing the break but not the content in that section.

    For example,

    The template is

    --- Section 1

    case name : << >>

    Date:  << >>      Sales name :      << >>                SalesID:  << >>

    --- Section  2

    Complaint Name : << >>    Order ID:  << >>  Product details : << >>

    If New Order then the Section 2 must be deleted, if existing one Section 1 must be deleted.

    previously if i removed  wx:sect node it would remove the content of that section also.

    Now i would like to implement the same in word 2010 - Open XML also.

    Kindly guide me on the same

    Tuesday, December 24, 2013 4:00 AM

Answers

  • Hi,

    You could use the following code to remove entire content of section part, it will remove all of the section content in word document, No matter paragraph or table .Hope it can help you:

     private void RemoveSectionBreaks(string filename)
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    List<ParagraphProperties> paraProps = mainPart.Document.Descendants<ParagraphProperties>()
                    .Where(pPr => IsSectionProps(pPr)).ToList();              
                    foreach (ParagraphProperties pPr in paraProps)
                    {
                        var p = pPr.Parent.PreviousSibling();
                        while (p != null && !IsSectionProps(p.GetFirstChild<ParagraphProperties>()))
                        {                        
                            var p1 = p.PreviousSibling();
                            p.Remove();
                            p = p1;
                        }
                        pPr.Parent .Remove();
                    }
    
                    mainPart.Document.Save();
                }
            }
            private bool IsSectionProps(ParagraphProperties pPr)
            {
                if (pPr != null)
                {
                    SectionProperties sectPr = pPr.GetFirstChild<SectionProperties>();
                    if (sectPr == null)
                        return false;
                    else
                        return true;
                }
                return false;
            }
    

    Regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 26, 2013 7:06 AM
    Moderator
  • Hi,

    You could use the Open XML SDK Productivity Tool CTP for Microsoft Office to open the word document and analyze the XML structure or reflect C# code. 

    #Open XML SDK 2.5 for Microsoft Office

    http://www.microsoft.com/en-in/download/details.aspx?id=30425

    The tools package contains the Open XML SDK v2.5 Productivity Tool for Office and the hyperlink to documentation for the Open XML SDK 2.5.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 27, 2013 8:57 AM
    Moderator

All replies

  • Hi,

    Based on my understanding, you want remove content of the section though Open XML SDK,As far as I know, WordprocessingML does not natively store the concept of section content, Instead it uses Sections Break to specify groups of paragraphs that have a specific set of page properties.
    For a workaround, you could remove section break and remove Previous Sibling node (Paragraph)
    Here is a sample for you reference:

    private void RemoveSectionBreaks(string filename)
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    List<ParagraphProperties> paraProps = mainPart.Document.Descendants<ParagraphProperties>()
                    .Where(pPr => IsSectionProps(pPr)).ToList();              
                    foreach (ParagraphProperties pPr in paraProps)
                    {
                        Paragraph p = pPr.Parent.PreviousSibling<Paragraph>();
                        p.Remove();
                        pPr.Parent .Remove();
                    }
                    mainPart.Document.Save();
                }
            }
            private bool IsSectionProps(ParagraphProperties pPr)
            {
                SectionProperties sectPr = pPr.GetFirstChild<SectionProperties>();
                if (sectPr == null)
                    return false;
                else
                    return true;
            }


    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 26, 2013 1:29 AM
    Moderator
  • Hi Marvin ,

    i convert the document and use in flat opc format and access each node with their nodes like SelectNodes(W:sectpr) .

    my single section will have more than one paragraph, tables etc.. when i delete a section .. it means i need to delete all the nodes pertaining to that section like w:tbl, w:P,etc..

    Earlier it was pretty easy..  the nodes pertaining to that section was under <wx:sect> node.  so all w:p, w:tbl etc... in that section had a parent node <wx:sect> .. so if i delete this node.. al the nodes ike w:p , w:tbl etc will be deleted.. hence the contents in the section gets deleted..

    Now if i remove the w:sectpr node.. it would remove only the properties of the section and not the entire section and its contents.

    Removal of Section break is just removing the Section division.

    But my aim is to remove the entire section and its contents.

    Thursday, December 26, 2013 4:38 AM
  • Hi,

    You could use the following code to remove entire content of section part, it will remove all of the section content in word document, No matter paragraph or table .Hope it can help you:

     private void RemoveSectionBreaks(string filename)
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    List<ParagraphProperties> paraProps = mainPart.Document.Descendants<ParagraphProperties>()
                    .Where(pPr => IsSectionProps(pPr)).ToList();              
                    foreach (ParagraphProperties pPr in paraProps)
                    {
                        var p = pPr.Parent.PreviousSibling();
                        while (p != null && !IsSectionProps(p.GetFirstChild<ParagraphProperties>()))
                        {                        
                            var p1 = p.PreviousSibling();
                            p.Remove();
                            p = p1;
                        }
                        pPr.Parent .Remove();
                    }
    
                    mainPart.Document.Save();
                }
            }
            private bool IsSectionProps(ParagraphProperties pPr)
            {
                if (pPr != null)
                {
                    SectionProperties sectPr = pPr.GetFirstChild<SectionProperties>();
                    if (sectPr == null)
                        return false;
                    else
                        return true;
                }
                return false;
            }
    

    Regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, December 26, 2013 7:06 AM
    Moderator
  • Hi ,

    But what if i have multiple w:p , w:tbl in a single section itself.

    For example,  4 paragaraphs, that is  4 <w:p> nodes..

    we delete the Section break node by ,

    node(0)= theNode.SelectNodes("following:w:Sectpr",nsmngr)

    node(0).parentnode.RemoveChild(node(0)

    Hence we remove the section break.

    It would be great if you can give sample solution in this format (open XML).

    Thursday, December 26, 2013 7:56 AM
  • Hi Preethi,

    This code will remove all of the node between two sections break,no matter how many paragaraphs or table in it.

    You should notice the following code from last my pasted:

                        var p = pPr.Parent.PreviousSibling();
                        while (p != null && !IsSectionProps(p.GetFirstChild<ParagraphProperties>()))
                        {                        
                            var p1 = p.PreviousSibling();
                            p.Remove();
                            p = p1;
                        }

    Regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Thursday, December 26, 2013 8:02 AM
    Moderator
  • Awesome !!!. just now noticed that..  but can you tell me how to approach the same with open XML rather than this C# code.
    Thursday, December 26, 2013 3:25 PM
  • Hi,

    You could use the Open XML SDK Productivity Tool CTP for Microsoft Office to open the word document and analyze the XML structure or reflect C# code. 

    #Open XML SDK 2.5 for Microsoft Office

    http://www.microsoft.com/en-in/download/details.aspx?id=30425

    The tools package contains the Open XML SDK v2.5 Productivity Tool for Office and the hyperlink to documentation for the Open XML SDK 2.5.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 27, 2013 8:57 AM
    Moderator