0

I'm having a problem using XML literals with a StringBuilder in VB 2008. If I use this code everything is fine.

Dim html As New System.Text.StringBuilder

html.Append(<html><body></body></html>)

MsgBox("hello")

Now the problem is I want to wrap HTML around something that is generated in code.

html.Append(<html><body>)

msgbox("nothing happens")

When the HTML doesn't have the corresponding ending tag, it acts like it goes beyond the ) and keeps looking for it.

Is there something I am doing wrong here?

4 Answers 4

2

I've never used VB's XML literals but I have built up a lot of XML. I like to use the StringWriter/XMLTextWriter classes:

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
    XmlTextWriter xw = new XmlTextWriter(sw);
    xw.WriteStartElement("html");
    xw.WriteStartElement("body");
    xw.WriteRaw(contentExp);
    ...
    wr.WriteEndElement();   // body
    wr.WriteEndElement();   // html
}
// do something with sb.ToString()?
Sign up to request clarification or add additional context in comments.

Comments

1

Because you're not forming a proper XML in your XML Literal statement (in your case you're not closing your tags), you can't use XML Literals here. You either need to have your XML literals be proper XML, or alternatively convert your code to use them as strings. Thus:

html.Append("<html><body>")

msgbox("nothing happens")

Comments

1

The obvious hunch would be that XML literals require well formed XML. If you want to wrap things, drop in an embedded expression as in...

html.Append(<html><body><%= contentExp %></body></html>)

Comments

1

Not an answer, but question back to you. What would be the value of using XML Literals with string builder. It seems at least to me that that goes against the grain. Create your XML using literals and then just get its string representation using the .ToString() method call if you need a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.