【问题标题】:Beginner LINQ to XML inline XML error初学者 LINQ to XML 内联 XML 错误
【发布时间】:2009-08-04 21:25:43
【问题描述】:
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

以上代码产生以下错误:

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") _
            )

上面的代码可以工作,但显然不如原来的那么优雅,我仍然希望能解释一下为什么我原来的语法是错误的。

【问题讨论】:

  • 我刚刚复制并粘贴了您的代码,它为我编译...

标签: asp.net linq


【解决方案1】:

代码对我来说可以正常工作,但也许可以尝试在新行上开始 XML 文字?

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

【讨论】:

  • 我不知道,但我的工作机器的代码有问题。我只是在家里尝试过,并同意你的看法,它没有任何问题。我必须仔细检查项目是否设置正确。
【解决方案2】:

我遇到了同样的问题,结果发现我的项目在 2.0 中运行,即使它设置为目标 3.5。

见:VB.NET XML Literal Expression Expected with Linq to XML

【讨论】:

    【解决方案3】:

    我不熟悉 VB 语法,但你不必做类似的事情

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

    【讨论】:

    • 这产生了同样的错误:Dim xml As New XElement(My Title Kyle)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多