0

I've been using stringbuilder for a long time and have never had this come up before. In this case I'm using a stringbuilder to put together a lengthy XML chunk, then at the end it gets put into a string to get submitted to a web service. At the ToString, all of the double quotes get turned into double-double quotes.

Dim tXML As New StringBuilder
Dim tt As String

tXML.Append("<?xml version=" & """" & "1.0" & """" & " encoding=" & """" & "UTF-8" & """" & "?>")
tt = tXML.ToString


'At this point the value of tXML is what I'd expect: 
    {<?xml version="1.0" encoding="UTF-8"?>}

'However the value of tt is
    "<?xml version=""1.0"" encoding=""UTF-8""?>"

'this method of double-quotes produces the same result
    tXML.Append("<?xml version=""1.0"" encoding=""UTF-8""?>")

This should be simple, but I'm at a loss. Thanks.

  • 1
    you could look at AppendFormat – Ňɏssa Pøngjǣrdenlarp Jul 05 '16 at 17:34
  • If you look at that string using the debugger its normal that you see these double-double quotes. They are not really there. – Steve Jul 05 '16 at 17:40
  • 2
    This is entirely normal, the debugger shows you the string the way you would (should) have written it in your source code. Click the spyglass icon to use the Text Visualizer. And you'll see that you don't have a real problem. – Hans Passant Jul 05 '16 at 17:42
  • Ah, you're right - the XML Visualizer shows it correctly. It was crashing in the web service, but apparently not because of a formatting error. This problem never came up before because it's not really a problem. Thanks. – JamesG17 Jul 05 '16 at 17:47
  • 3
    Btw, there is no need for breaking the string. You can just write `" – Nico Schertler Jul 05 '16 at 18:03
  • It would seem the `XDocument`, `XElement`, etc classes. would be better suited to creating xml than string concatenation. You would then not need to even worry about quotation marks. And since you are using VB, even XML literals might be useful. – Chris Dunaway Jul 06 '16 at 13:52

1 Answers1

0

Using a StringBuilder to assemble Xml doesn't seem like the best method. Why not use the Xml classes available along with VB.Net's XML literals?

Imports System.Xml.Linq

Private Sub createXml()

    Dim xDoc As New XDocument()

    xDoc.Declaration = New XDeclaration("1.0", "utf-8", "yes")

    xDoc.Add(<Root attr="x">
                <SomeElement>Some Value</SomeElement>
            </Root>)

    xDoc.Save("c:\somefolder\myfile.xml")

End Sub
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48