0

A while back, I asked this question:

Does VB.NET have a multi-line string declaration syntax equivalent to c#?

Here I was introduced to XML literals in VB.NET. Using this syntax, I was able to simulate the multi-line string syntax available in c# using the @ symbol. However, I've come upon a snag. It seems that putting < or > in the text does not sit well in the belly of Visual Studio. Take this code as an example:

Dim Sql As String = <a><![CDATA[]]>
                        <text instead pointy brackets fails>
                    </a>.Value

Can I somehow escape these brackets or tell the literal not to care about it?

Community
  • 1
  • 1
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206

2 Answers2

4

XML literals are only for XML data. You could use &lt; to escape the left angle brackets, but really unless you genuinely want XML data I would suggest you don't use XML literals at all.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the answer, but definitely not what I wanted to hear =\. Since the whole point of using XML literal was to emulate c#'s multi-line string syntax using `@`, replacing all `<` with `<` would defeat the purpose of convenience. I ended up using a resource file which ultimately does what I need. – oscilatingcretin Sep 24 '12 at 11:38
  • That may be the point of *your use* of XML literals - but it's emphatically *not* the purpose of the feature. It sounds like putting it in a resource file is the right way to go, really. – Jon Skeet Sep 24 '12 at 11:49
2

You need to put the text within the CDATA section:

Dim Sql As String = <a><![CDATA[
<text instead pointy brackets fails>
]]></a>.Value

Console.WriteLine("===")
Console.WriteLine(Sql)
Console.WriteLine("===")

should output:

===

<text instead pointy brackets fails>

===

Note that CDATA will also preserve leading spaces in your text; that is why I removed them from the bracketed text.

AlGonzalez
  • 151
  • 2
  • 3
  • @oscilatingcretin: It's still not appropriate though, IMO. If you're not trying to create XML, you shouldn't be using an XML literal. – Jon Skeet Apr 17 '13 at 14:28
  • @JonSkeet, my intentions, appropriate or not, were made crystal clear in my question. If you find my "coding hack" to be inappropriate, you shouldn't have contributed to my blatent misuse of the .NET framework by posting your own solution as an anwer. While a resource file may be the best-practice approach, this approach gives you the C#-style, multi-line string construct which I really miss while I am coding in VB.NET. Unless anyone can come up with an *objective* reason for why this hack approach is bad (performance, etc), I don't have a problem using it. – oscilatingcretin Apr 17 '13 at 17:30
  • @oscilatingcretin: Well for one thing, it'll fail if your multi-line text ever needs to contain "]]>". My own answer *specifically* suggested that you stop doing this, so I don't see how you can suggest that it "contributed" to your blatant misuse of VB. But hey, if you want to keep knowingly abusing something, there's nothing I can do to stop you, obviously. At least you're now fully aware that you *are* abusing this. – Jon Skeet Apr 17 '13 at 20:10