I am trying to learn about XML literals in VB. In a "learning" project which based on a console application template the following compiles and runs without error (and without any special Imports statements nor references added):
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
End Module
Yet I tried repeating the same in a new project based on a Windows Forms app and I get errors on the last 2 statements (both saying only "Declaration expected"; also note: I compared the References for the console app with those provided by default for the winforms app and added System.Data.Linq then given the error I added 2 Imports statements but I still get the errors).
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
Please tell me what I am missing. Thank you.
Your second block of code is not inside of a method. It needs to be inside of a Sub
or 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
And then at some point in your application something will call SomeFoo
.