2009-08-04 32 views
1
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 

上面的代碼產生以下錯誤:初學者的LINQ to XML內聯XML錯誤

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 

爲什麼?

編輯:

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

上面的代碼工作,但顯然不如原來的優雅和我仍然會欣賞爲什麼我原來的語法是錯誤的解釋。

+0

我只是複製並粘貼您的代碼,並將其編譯爲我... – 2009-08-04 22:54:04

回答

1

的代碼工作正常,我原樣,但也許嘗試啓動XML文本上新線?

Dim xml As XElement = _ 
      <book> 
       <title>My Title</title> 
       <author>Kyle</author> 
       <publisher>WROX</publisher> 
      </book> 
+0

我不知道,但我的工作機器有該代碼的問題。我只是在家裏嘗試過,並且同意你的看法,沒有任何問題。我將不得不再次檢查項目是否正確安裝。 – 2009-08-04 23:19:36

-1

我不熟悉VB語法,但不要你必須做類似

Dim xml as XElement = new XElement(<book>... etc)? 
+0

這產生了同樣的錯誤: 昏暗的XML作爲新的XElement(我的標題凱爾) – 2009-08-04 21:31:32