0

我正在尝试了解 VB 中的 XML 文字。在基于控制台应用程序模板的“学习”项目中,以下编译和运行没有错误(并且没有添加任何特殊的 Imports 语句或引用):

Module Module1
Sub Main()
    Dim db As New AdventureWorksDataContext
    Dim stores = <?xml version="1.0"?>
                 <stores>
                     <%= From vStoreWithDemographics In db.vStoreWithDemographics _
                         Select <store>
                                    <ID>
                                        <%= vStoreWithDemographics.BusinessEntityID %>
                                    </ID>
                                    <Name>
                                        <%= vStoreWithDemographics.Name %>
                                    </Name>
                                    <AnnualSales>
                                        <%= vStoreWithDemographics.AnnualSales %>
                                    </AnnualSales>
                                    <BankName>
                                        <%= vStoreWithDemographics.BankName %>
                                    </BankName>
                                    <SquareFeet>
                                        <%= vStoreWithDemographics.SquareFeet %>
                                    </SquareFeet>
                                    <NumberEmployees>
                                        <%= vStoreWithDemographics.NumberEmployees %>
                                    </NumberEmployees>
                                </store>
                     %>
                 </stores>
    stores.Save("c:\stores.xml")
    Process.Start("c:\stores.xml")
End Sub

端模块

然而,我尝试在基于 Windows 窗体应用程序的新项目中重复相同的操作,但在最后 2 个语句中出现错误(两者都只说“预期声明”;另请注意:我将控制台应用程序的参考资料与默认提供的参考资料进行了比较对于 winforms 应用程序并添加 System.Data.Linq 然后给出错误我添加了 2 Imports 语句但我仍然得到错误)。

Imports System.Diagnostics
Imports System.Xml.Linq.XDocument
Public Class Form1
Dim db As New AdventureWorksDataContext
Dim stores = <?xml version="1.0"?>
             <stores>
                 <%= From vStoreWithDemographics In db.vStoreWithDemographics _
                     Select <store>
                                <ID>
                                    <%= vStoreWithDemographics.BusinessEntityID %>
                                </ID>
                                <Name>
                                    <%= vStoreWithDemographics.Name %>
                                </Name>
                                <AnnualSales>
                                    <%= vStoreWithDemographics.AnnualSales %>
                                </AnnualSales>
                                <BankName>
                                    <%= vStoreWithDemographics.BankName %>
                                </BankName>
                                <SquareFeet>
                                    <%= vStoreWithDemographics.SquareFeet %>
                                </SquareFeet>
                                <NumberEmployees>
                                    <%= vStoreWithDemographics.NumberEmployees %>
                                </NumberEmployees>
                            </store>
                 %>
             </stores>
    stores.Save("c:\stores.xml")                   'error here
    Process.Start("c:\stores.xml")                 'error here
End Class

请告诉我我错过了什么。谢谢你。

4

1 回答 1

1

您的第二个代码块不在方法内。它需要在 a Subor内Function

 Public Class Form1
      Public Sub SomeFoo()
           Dim db As New AdventureWorksDataContext
           Dim stores = <?xml version="1.0"?>
                           <stores>
                                <%= From vStoreWithDemographics In db.vStoreWithDemographics _
                                     Select <store>
                                                   <ID>
                                                        <%= vStoreWithDemographics.BusinessEntityID %>
                                                   </ID>
                                                   <Name>
                                                        <%= vStoreWithDemographics.Name %>
                                                   </Name>
                                                   <AnnualSales>
                                                        <%= vStoreWithDemographics.AnnualSales %>
                                                   </AnnualSales>
                                                   <BankName>
                                                        <%= vStoreWithDemographics.BankName %>
                                                   </BankName>
                                                   <SquareFeet>
                                                        <%= vStoreWithDemographics.SquareFeet %>
                                                   </SquareFeet>
                                                   <NumberEmployees>
                                                        <%= vStoreWithDemographics.NumberEmployees %>
                                                   </NumberEmployees>
                                              </store>
                                %>
                           </stores>
           stores.Save("c:\stores.xml")
           Process.Start("c:\stores.xml")
      End Sub
 End Class

然后在您的应用程序中的某个时间点会调用SomeFoo.

于 2012-03-27T17:11:59.597 回答