2012-03-27 69 views
0

我想了解VB中的XML文字。其中基於控制檯應用程序模板下面的編譯和無差錯運行「學習」項目(和不添加任何特殊的進口報表,也未引用):VB.Net中System.Xml.Linq.XDocument聲明期望錯誤

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 Forms應用程序的新項目中也是如此,並且我在最後2條語句中遇到了錯誤(兩個語句都只說「聲明預期爲」;另請注意:我將控制檯應用程序的參考與默認提供的Winforms應用程序並添加System.Data.Linq然後給出錯誤我添加了2個進口語句,但我仍然得到錯誤)。

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 

請告訴我我缺少什麼。謝謝。

回答

1

你的第二塊代碼不在方法內。它需要一個SubFunction內:

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