Can I remove text from a StringWriter ?

H

Harry Haller

Can I remove text from a StringWriter ?

I have a StringWriter object, the content of which looks like this:

"<div>Some other html which I wish to keep</div>"

Can I remove the unwanted <div>, </div> tags? i.e. remove the last 4
characters and the first 3.

The context in which the object occurs, in asp.net code, is:

StringWriter sw = new StringWriter();
HtmlTextWriter htmlTw = new HtmlTextWriter(sw);
gvExcel.RenderControl(htmlTw);
Response.Write("<html>\r\n<head>\r\n");
Response.Write("<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=UTF-8\" />\r\n");
Response.Write("</head>\r\n<body>\r\n");
Response.Write(sw.ToString());
Response.Write("\r\n</body>\r\n</html>");

This code is used to write a html file with a .xls extension which can
be loaded into excel.

I don't want the dummy <div> to be there .

gvExcel is an asp.net GridView control.
 
N

Nicholas Paldino [.NET/C# MVP]

Harry,

StringWriter derives from TextWriter, which (in a logical way) is meant
to provide a write once approach to writing text. A good example of
write-once semantics is a network stream, you can't change what you sent
once you sent it.

StringWriter is an implementation of TextWriter, but uses a
StringBuilder as the storage mechanism for the text being written. With
that in mind, once you are done using your StringWriter to write your text,
you can call the GetStringBuilder method and process the text in that before
you return it to the client.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top