Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Attribute value in VB.NET XML Literals

Tags:

xml

vb.net

As basic as this is, it took me a few minutes to figure out, so wanted to share with the rest of the community to avoid anyone else from wasting their time.

I am attempting to generate the following XML string using VB.NET XML Literals

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

I wrote the code like this (Assume Books is just an Enumerable of String),

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

But the compiler is complaining about the quotes that are supposed to surround the attribute value. I tried using single quotes, two double quotes, nothing works.

like image 907
Justin Largey Avatar asked Nov 26 '25 22:11

Justin Largey


1 Answers

After some quick experimentation, I figured out that you need to remove the quotes altogether, so the code looks like:

Dim output = <Books>
    <%= From book In Books _
    Select _
    <Book Name=<%= book %>/> %>
    </Books>
like image 91
Justin Largey Avatar answered Nov 29 '25 17:11

Justin Largey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!