初学者的LINQ to XML内联XML错误 [英] Beginner LINQ to XML inline XML error

查看:118
本文介绍了初学者的LINQ to XML内联XML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以上code是生产以下错误:

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

为什么?

编辑:

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

上面code ++工程,但显然并不像原来那样优雅,我仍然AP preciate为什么我原来的语法是错误的解释。

推荐答案

在code工作正常,我原样,但也许尝试启动XML文本在新行?

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>

这篇关于初学者的LINQ to XML内联XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆