2011-11-11 17 views
1

XMLにコンテンツを解析する前に、.docxファイルから空の段落を削除しようとしています。どうすればこれを達成できますか?Open XML 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

問題が発生している可能性がある問題は、各ブロックのaのリストから項目を削除することです。あなたは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 
関連する問題