An object oriented way of writing HTML as a string?

I am trying to programmatically send replicas by e-mail of all dll files and their versions to a directory. I would like to send a message as HTML output using a table. Is there a good object oriented way to do this? I would not want to write all tags manually.

Sort of:

private string getHTMLString()
{
    DirectoryInfo di = new DirectoryInfo("some directory");
    FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);
    foreach (FileInfo file in files)
    {
        Assembly assembly = Assembly.LoadFile(file.FullName);
        string version = assembly.GetName().Version.ToString();
    }
 }
+3
source share
8 answers

Something like that?

private string getHTMLString()
{
    DirectoryInfo di = new DirectoryInfo("some directory");
    FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);
    StringBuilder sb = new StringBuilder();
    sb.Append("<table>");
    foreach (FileInfo file in files)
    {
        Assembly assembly = Assembly.LoadFile(file.FullName);
        string version = assembly.GetName().Version.ToString();
        sb.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", file.FullName, version);
    }
    sb.Append("</table>");
    return sb.ToString();

 }

Not really “object oriented,” but I would say that it’s the most logical.

DISCLAIMER: Compiled manually

+5
source

OO, System.Web.UI.WebControls.Table, TableRows TableCells, Table.RenderControl HtmlTextWriter, , : :)

        var tbl = new System.Web.UI.WebControls.Table();

        DirectoryInfo di = new DirectoryInfo("some directory");
        FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);
        foreach (FileInfo file in files)
        {
            Assembly assembly = Assembly.LoadFile(file.FullName);
            string version = assembly.GetName().Version.ToString();

            var tr = new System.Web.UI.WebControls.TableRow();
            var tc = new System.Web.UI.WebControls.TableCell();
            tc.Text = HttpUtility.HtmlEncode(version);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);
        }

        using (var ts = new StringWriter())
        using (var html = new System.Web.UI.HtmlTextWriter(ts))
        {
            // Not entirely sure about this part
            tbl.RenderControl(html);
            html.Flush();
            string htmlString = ts.ToString();
        }
+8

HtmlTextWriter HtmlTextWriterTag , "<" " > ".

:

StringBuilder sb = new StringBuilder();

using (HtmlTextWriter w = new HtmlTextWriter(new StringWriter(sb)))
{
      w.RenderBeginTag(HtmlTextWriterTag.P);

      w.AddStyleAttribute(HtmlTextWriterStyle.Color, "red");

      w.RenderBeginTag(HtmlTextWriterTag.Span);

      w.Write("This is some text");

      w.RenderEndTag();

      w.RenderEndTag();
 }

 string html = sb.ToString();

HTML , HTML " OO", ( ) HTML ( ..).

+7

, LINQ to XML HTML... , LINQ XElement .

, - , HTML. , TD: new XElement("td") <td/>, HTML. , : new XElement("td", String.Empty) - <td></td>.

private string GetHtmlString()
{
    DirectoryInfo di = new DirectoryInfo("some directory");
    FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);

    var container = new XElement("table",
        from file in files
        let assembly = Assembly.LoadFile(file.FullName)
        select new XElement("tr", 
            new XElement("td", file.FullName),
            new XElement("td", assembly.GetName().Version.ToString())
        )
    );

    return container.ToString();
 }
+7

- . .

HtmlTextWriter , .

+2

, , , . , , StringTemplate.

+1

XmlWriter, OO-:

StringBuilder sb = new StringBuilder();
XmlWriter xmlWri = XmlWriter.Create(sb);
xmlWri.WriteStartElement("html");
{
    xmlWri.WriteStartElement("body");
    {
        xmlWri.WriteAttributeString("bgcolor", "black");

        // More html stuff
    }
    xmlWri.WriteEndElement(); // body
}
xmlWri.WriteEndElement(); // html
xmlWri.Flush();
xmlWri.Close();
string html = sb.ToString();

: , , HTML, . HTML- , , / HTML.

+1

I am currently writing a library that exactly solves this problem. It allows you to create HTML with collection initializers, so this can be pretty brief. It also supports CSS styles, element attributes as optional parameters, etc. This is LGPL'd, so you can use it anywhere.

Your code will look like this:

private string getHTMLString()
{
    DirectoryInfo di = new DirectoryInfo("some directory");
    FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);

    var table = new Table();

    foreach (FileInfo file in files)
    {
        Assembly assembly = Assembly.LoadFile(file.FullName);
        string version = assembly.GetName().Version.ToString();

        table.Add(new Tr { new Td { file.FullName }, new Td { version } });
    }

    return table.Render();
}

HTML ++ at SourceForge .

0
source

Source: https://habr.com/ru/post/1727304/


All Articles