如何在CDATA部分中参数化数据

时间:2016-07-29 14:33:19

标签: xml vb.net cdata

我知道我可以在vb.net中将文本添加到CDATA部分:

Dim temp As System.Xml.Linq.XCData = <![CDATA[Hello World!]]>

但是如何传递参数值:

Dim myValue as string = "Hello World!"
Dim temp As System.Xml.Linq.XCData = <![CDATA[myValue]]>

我知道我可以这样做,但这是正确的做法吗?

Dim myValue as string = "Hello World!"
Dim temp As New System.Xml.Linq.XCData("<![CDATA[" & myValue & "]]>")

2 个答案:

答案 0 :(得分:1)

简单地将字符串值赋给XCData构造函数il将自己放置CDATA标记

    Dim myValue As String = "Hello World!"
    Dim temp As New XCData(myValue)

答案 1 :(得分:-1)

它的工作原理如下:

Dim myValue as string = "Hello World!"
Dim temp As System.Xml.Linq.XCData = <![CDATA[<%= myValue %>]]>

供参考:MSDN (How to: Embed Expressions in XML Literals (Visual Basic))

编辑:因此不起作用,因为CDATA不允许任何嵌入式表达式。请参阅:MSDN (XML CDATA Literal (Visual Basic))

所以你留下XCData的构造函数:

Dim myValue As String = "Hello World!"
Dim temp As New XCData(myValue)

您可以同时使用两者。

visual basic编译器实际上只是转换了像

这样的CDATA文字
Dim temp = <![CDATA[foo]]>

Dim temp = New XCData("foo")