Question

I was advised to use XMLwriter to build HTML documents in order to display them in webbrowser object. Creating doctype and startelements like HTML,BODY is OK..but I am experiencing 2 main problems:

  1. I cannot add tags like <br>. Using WriteString skips < and >.
  2. The output string is one line - I would need something like writeLine. You know, when I display source its all in the first line.

Thanks

Was it helpful?

Solution

HTML is not a valid XML format, as you are discoving with tags like <img ...>

You could create XHTML, which is XML compliant (specify this in your DOCTYPE)

In XHTML single tags are writen like this <br /> for example

 HTML: <img src="..">
 XHTML: <img src=".." />

This link might be helpful XHTML vs HTML

Whitespace layout is nice for humans to read, but makes no difference to how the browser renders the Xhtml. In fact stripping unnecessary whitespace will produce slightly smaller files.

OTHER TIPS

You could use the Indent property:

var settings = new XmlWriterSettings();
settings.Indent = true;
using (var writer = XmlWriter.Create(outputStream, settings))
{
    writer.WriteDocType("html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", null);
    writer.WriteStartElement("html");
    writer.WriteStartElement("body");
    writer.WriteStartElement("b");
    writer.WriteValue("Test");

    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndElement();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top