简体   繁体   English

C#如何将Console.Writeline输出保存为HTML格式?

[英]C# How to save Console.Writeline Outputs to HTML Format?

I have a program which outputs various results after greping strings from a log text file. 我有一个程序,可以从日志文本文件中提取字符串后输出各种结果。 The results are shown onto a command line console. 结果显示在命令行控制台上。

Therefore are there anyways to generate the various output results into a html based report? 因此,是否有将各种输出结果生成为基于html的报告的方法?

What methods could be used to convert the outputs? 可以使用哪些方法转换输出? HtmlTextWriter? HtmlTextWriter? What are the steps needed to convert the file? 转换文件需要什么步骤?

May someone please advise on the codes to do so? 有人可以建议这样做吗? Thanks! 谢谢!

The codes: 代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Diagnostics;
 using System.IO;
 using System.Text.RegularExpressions;
 using System.Web.UI; // The system is telling me that it does not exist in the namespace System.web....


        foreach (String r in lines.Skip(1))
        {
            String[] token = r.Split(',');

            String[] datetime = token[0].Split(' ');

            String timeText = datetime[4];

            Double rawSize = Convert.ToDouble(token[1]);

            Double fileSize = ((rawSize / 1024) / 1024);

            String actions = token[2];

            String fileName = token[7];

            if ((Path.GetExtension(token[7]).Contains(".exe") || (Path.GetExtension(token[7]).Contains(".msi"))))
            {

                Console.WriteLine("The time for this array is: " + timeText);

                Console.WriteLine(token[7]);

                Console.WriteLine(actions);

                Console.WriteLine(fileSize);

                ProgramFilter(actions, k, fileName);

                x = 1;

                Console.WriteLine("================================================");

            }

} }

HTML is a text based format, so you can simply output HTML. HTML是基于文本的格式,因此您可以简单地输出HTML。

There isn't anything that will simply convert text into HTML for you - you need to decide what the markup will be. 没有什么可以简单地将文本转换为HTML的-您需要确定标记是什么。

There are many options to do this: 有很多选项可以做到这一点:

  • Using ASP.NET as a template and reading the result 使用ASP.NET作为模板并读取结果
  • Using a StringBuilder to build up your HTML 使用StringBuilder建立HTML
  • Use HtmlTextWriter to build up the HTML 使用HtmlTextWriter构建HTML
  • Convert the data to XML and transform it with XSLT 将数据转换为XML并使用XSLT进行转换

As for writing to a file - again, several ways to do this: 至于写入文件-再次,有几种方法可以做到这一点:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM