tags:

views:

111

answers:

3
Imports System.Xml.Linq
Imports System.Linq

Partial Class test2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xml As XElement = <book>
                                  <title>My Title</title>
                                  <author>Kyle</author>
                                  <publisher>WROX</publisher>
                              </book>
    End Sub    
End Class

The above code is producing the following error:

Compiler Error Message: BC30201: Expression expected.

Source Error:

Line 8:  
Line 9:      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Line 10:         Dim xml As XElement = <book>
Line 11:                                   <title>My Title</title>
Line 12:                                   <author>Kyle</author>


Source File: C:\Inetpub\wwwroot\myproject\web\test2.aspx.vb    Line: 10

Why?

edit:

Dim xml As XElement = New XElement("book", _
                New XElement("title", "My Title"), _
                New XElement("author", "Kyle"), _
                New XElement("publisher", "WROX") _
            )

The above code works, but obviously is not as elegant as the original and I would still appreciate an explanation of why my original syntax is wrong.

A: 

I'm not familiar with the VB syntax but don't you have to do something like

Dim xml as XElement = new XElement(<book>... etc)?
Orion Edwards
This produced the same error:Dim xml As New XElement(<contact><title>My Title</title><author>Kyle</author></contact>)
Kyle B.
+1  A: 

The code works fine for me as is, but maybe try starting the XML literal on a new line?

    Dim xml As XElement = _
            <book>
                <title>My Title</title>
                <author>Kyle</author>
                <publisher>WROX</publisher>
            </book>
dahlbyk
I have no idea, but my work machine is having a problem with that code. I just tried it at home and agree with you that there is nothing wrong with it. I'll have to double check the project is setup properly.
Kyle B.
+1  A: 

I had the same issue and as it turned out my project was running in 2.0 even though it was set to target 3.5.

See: http://stackoverflow.com/questions/3007118/vb-net-xml-literal-expression-expected-with-linq-to-xml/

Jim