Search code examples
c#browserxmlwriter

XMLWriter for HTML creation - how to add simple non-pair tags?


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


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.