开发者

Value of type 'System.Collections.ArrayList' cannot be converted to 'System.Collections.Generic.List(Of iTextSharp.text.IElement)'

开发者 https://www.devze.com 2023-04-06 04:41 出处:网络
I\'m having a problem with this code in the highlighted line(*); getting the error in the heading. Any help would be appreciated as I cant get the solution.

I'm having a problem with this code in the highlighted line(*); getting the error in the heading. Any help would be appreciated as I cant get the solution.

Dim htmlarraylist As New List(Of iTextSharp.text.IElement)
htmlarraylist = *HTMLWorker.ParseToList(New StreamReader(tempFil开发者_JAVA百科e), New StyleSheet())*
document.Open()

For Each element As iTextSharp.text.IElement In htmlarraylist
   document.Add(element)
Next

document.Close()


Right, so it sounds like ParseToList returns a non-generic ArrayList. The simplest way of fixing that - assuming .NET 3.5 - is probably to use Cast and ToList:

Dim htmlarraylist As List(Of iTextSharp.text.IElement)
htmlarraylist = HTMLWorker.ParseToList(New StreamReader(tempFile), _
                                       New StyleSheet()) _
                          .Cast(Of iTextSharp.text.IElement) _
                          .ToList

Note how I've also change the first statement so it no longer creates an instance of List(Of T) only to immediately throw it away.

That's assuming you really want it as a List(Of T). If you don't mind it just been an ArrayList, you can just change the declaration:

Dim list As ArrayList = HTMLWorker.ParseToList(New StreamReader(tempFile), _
                                               New StyleSheet())

You're only iterating over it in the code you've shown, after all. (You could equally change the declaration to use IEnumerable.)


Shorten your code to just

document.Open()  
For Each element As iTextSharp.text.IElement In HTMLWorker.ParseToList(New StreamReader(tempFile), New StyleSheet())    
  document.Add(element) 
Next
document.Close() 
0

精彩评论

暂无评论...
验证码 换一张
取 消