我在VB.net中使用VS.net 2008有一个程序,它将获取订单列表并使用下面的代码中显示的订单列表构建一个XML文件:
随着订单数量的增加,我想为每500个订单构建XML文件
Dim Orders as List(of Orders)=DAL.GetAllOrders()
Dim Order as new Orders
Public xmlstring As New StringBuilder
If Not Orders Is Nothing Then
xmlstring.Append("<?xml version=""1.0"" encoding=""Windows-1252"" standalone=""yes""?>")
xmlstring.Append("<Orders>")
For Each Order In Orders
'Build the XML File
next
'access web service and pass the XML file
end if
不是为所有记录构建XML,而是为每500条记录创建XML。 我尝试了以下代码,它正在抛出一个错误说 表达式是Orders类型,它不是集合类型。
请帮忙
Dim start As Integer
For i As Integer = start To Orders.Count Step 500
xmlstring.Append("<?xml version=""1.0"" encoding=""Windows-1252"" standalone=""yes""?>")
xmlstring.Append("<Orders>")
For Each Order In Orders(i)
'Build the XML File
next
next
答案 0 :(得分:1)
要解决的一些问题:
循环构造:
Dim Orders as List(of Order) = DAL.GetAllOrders()
If Not Orders Is Nothing Then
Dim i As Integer = 1
For Each order As Order In Orders
If i=500 Then
' write XML footers, close the current file
' open the next file, write XML header
i = 1
Else
i += 1
End If
' write XML for this order
Next
' Write final XML footer and close current file
End If
答案 1 :(得分:0)
它抛出错误,表示Expression的类型为Orders,而不是集合类型。
变量“Orders”是一个集合类型(List(of Orders)),但是Orders(i)不是,它是一个“Orders”(不管是什么,你还没有发布定义)对象。
因此,您需要在Orders类中实现IEnumerable或更改此行:
For Each Order In Orders(i)
答案 2 :(得分:-1)
这样的事情怎么样:
Dim OrdersList as List(of Orders)=DAL.GetAllOrders()
For i as int32 = 0 to OrdersList.Count() - 1
Dim o as Orders = OrdersList(i)
'Build your xml
If i mod 500 = 0 Then
'write out xml & reset your string builder
End If
Next
*注意我更改了您的Orders变量,因为命名约定有点令人困惑,可能导致名称冲突。
答案 3 :(得分:-1)
我会用这样的东西:
for i as integer = 0 to orders.count - 1
<create xml here and put into variable (append new XML as needed)>
if (i mod 500 = 0 andalso not i = 0) orelse i = orders.count - 1 then
<output XML here and clear XML variable>
end if
next i