2011-11-11 98 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