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