OrElse
OrElse

Reputation: 9969

Can i embed LINQ results into CDATA?

Can i merge the values returned from a LINQ query (in the next example c.day) with CDATA like...

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

Upvotes: 2

Views: 257

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167706

I don't think you can use CDATA section literals and embedded expressions inside them with VB.NET but you can certainly construct a CDATA section node with new XCData() as in the following example:

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>

That serializes as

<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>

Upvotes: 3

Related Questions