2009-08-04 25 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