views:

65

answers:

2

Hi, 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

A: 

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.

Dead account
A: 

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();
}
Darin Dimitrov
This will write <b /> which works for Xhtml but and only "by co-incidence" in html
Dead account
Also indent only effects the text output, which has no effect on how the xhtml is rendered.
Dead account
I know I will need only to have more readable code. Thanks
Petr