我可以将LINQ结果嵌入CDATA吗?

时间:2011-01-03 17:33:42

标签: html xml vb.net linq-to-xml cdata

我可以将LINQ查询(在下一个示例c.day中)返回的值与CDATA合并...

Dim Result = <items>
             <%= From c In db.News Select _
              <item>
                <day><![CDATA[<font size="30" color="#7CBEBD"><%= c.day %></font>]]></day>
              </item> %>
            </items>

1 个答案:

答案 0 :(得分:3)

我认为您不能在VB.NET中使用CDATA部分文字和嵌入式表达式,但您可以使用new XCData()构建CDATA部分节点,如下例所示:

Dim words As String() = {"foo", "bar", "baz"}
Dim doc As XDocument =
    <?xml version="1.0"?>
    <root>
        <items>
            <%= From word In words
                Select <item>
                           <%= New XCData("<font size=""3"">" + word + "</font>") %>
                       </item>
            %>
        </items>
    </root>

序列化为

<root>
  <items>
    <item><![CDATA[<font size="3">foo</font>]]></item>
    <item><![CDATA[<font size="3">bar</font>]]></item>
    <item><![CDATA[<font size="3">baz</font>]]></item>
  </items>
</root>