2011-11-11 2 views
1

XML로 내용을 파싱하기 전에 .docx 파일에서 빈 단락을 제거하려고합니다. 나는 이것을 어떻게 얻을 수 있을까?OpenXML SDK 2.0을 사용하여 .docx에서 빈 단락을 제거하십시오.

Protected Sub removeEmptyParagraphs(ByRef body As DocumentFormat.OpenXml.Wordprocessing.Body) 
    Dim colP As IEnumerable(Of Paragraph) = body.Descendants(Of Paragraph)() 

    Dim count As Integer = colP.Count 
    For Each p As Paragraph In colP 
     If (p.InnerText.Trim() = String.Empty) Then 
      body.RemoveChild(Of Paragraph)(p) 
     End If 
    Next 
End Sub 

답변

1

문제는 각 블록에 대한 목록에서 항목을 제거하는 것입니다. linq 및 RemoveAll 메서드를 사용해 볼 수 있습니다.

Protected Sub removeEmptyParagraphs(ByRef body As DocumentFormat.OpenXml.Wordprocessing.Body) 
    Dim colP As IEnumerable(Of Paragraph) = body.Descendants(Of Paragraph)() 
    colP.RemoveAll(Function(para) para.InnerText.Trim() = String.Empty) 
End Sub 
관련 문제