Setting an attribute value in VB.NET XML literals

As I understand it, it took me a few minutes to figure it out, so I wanted to share with the rest of the community so that no one wasted time wasting time.

I am trying to generate the following line of XML using VB.NET XML literals

<Books>
    <Book Name="The First Book" />
    <Book Name="The Second Book" />
</Books>

I wrote code like this (suppose books are just an enumerated string)

Dim output = <Books>
    <%= From book In Books _
    Select _
    <Book Name="<%= book %>"/> %>
    </Books>

But the compiler complains about the quotation marks that should surround the attribute value. I tried using single quotes, two double quotes, nothing works.

+3
source share
1 answer

After a few quick experiments, I realized that you need to completely remove the quotes, so the code looks like this:

Dim output = <Books>
    <%= From book In Books _
    Select _
    <Book Name=<%= book %>/> %>
    </Books>
+4
source

Source: https://habr.com/ru/post/1709862/


All Articles