Monday, June 28, 2010

Setting Attribute value in VB.NET XML Literals

ms_net_rgb_webYou the receive compile error when using XML literals and try to set an attribute using quotation marks…

Error message: "Expected matching closing double quote for XML attribute value."

Your code might look similar to this fragment:

Results in an error:

Dim output = From item In items
  Select <Item ID="<%= item.ID %>">
  </Item>

What's wrong? Just Remove the quotation marks. The <% %> syntax will insert them later.

Works fine:

Dim output = From item In items
  Select <Item ID=<%= item.ID %>>
  </Item>

The result as XML:

<Item ID="1">
</Item>
<Item ID="2">
</Item>

1 comment:

Anonymous said...

Thank you! Saved me some time and aggravation.