简体   繁体   English

如何在现有 HTML 文件的末尾添加表格?

[英]How to add a table at the end of the existing HTML file?

I use in VS2017, C# with .net framework 4.5 in window 7 to build a winform GUI program.我在 VS2017 中使用 C# with .net framework 4.5 in window 7 来构建一个 winform GUI 程序。

I have a existing HTML file with figures in it.我有一个现有的 HTML 文件,其中包含数字。 I want to add a table to the end of the HTML file.我想在 HTML 文件的末尾添加一个表格。

The data of the table is from user input at the GUI interface(Textbox) and socket client program.该表的数据来自用户在 GUI 界面(文本框)和套接字客户端程序的输入。

I have tried StreamReader and StreamWriter method but I do not know how to generate the table, add the table without altering previous content in the HTML file and receive data from socket client to fill the table.我尝试过 StreamReader 和 StreamWriter 方法,但我不知道如何生成表格,在不改变 HTML 文件中先前内容的情况下添加表格并从套接字客户端接收数据以填充表格。

The Html file is in the same folder as the C# .exe program built. Html 文件与构建的 C# .exe 程序位于同一文件夹中。

Also, the error of 'the type of namespace name IPAddress could not be found' occurs when I create the local ipaddress to get data to fill the table using socket.此外,当我创建本地 ipaddress 以获取数据以使用套接字填充表时,会发生“找不到命名空间名称 IPAddress 的类型”的错误。

public partial class Form1 : Form
{
            ThreadStart Testref = new ThreadStart(socketThread);

            Thread socketThread = new Thread(Testref);
            socketThread.Start();
}

public static void socketThread()
{
        Form1 frm1 = new Form1();
        Console.WriteLine("Socket thread starts");
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        TcpListener serverSocket = new TcpListener(ipAddress, 8021);
        TcpClient clientSocket = default(TcpClient);
        int counter = 0;

        serverSocket.Start();
        Console.WriteLine(" >> " + "Server Started");

        counter = 0;
        while (true)
        {
            counter += 1;
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> " + "Client No:" + 
     Convert.ToString(counter) + " started!");
            handleClinet client = new handleClinet();
            client.startClient(clientSocket, Convert.ToString(counter));
        }

        clientSocket.Close();
        serverSocket.Stop();
        Console.WriteLine(" >> " + "exit");
        Console.ReadLine();
}

to answer the last part of the question " Also, the error of 'the type of namespace name IPAddress could not be found' " you need to put using System.Net;回答问题的最后一部分“此外,'找不到命名空间名称 IPAddress 的类型'”的错误,您需要using System.Net; on the top.在顶端。 For rest of the issue, you can load the Html file content into a HtmlDocument and then create a HtmlElement then use HtmlElement.AppendChild to add new element into Html document post that you can write that Html Document in that file again by extracting the doc.documentElement.outerHTML;对于剩下的问题,您可以将 Html 文件内容加载到HtmlDocument 中,然后创建一个HtmlElement,然后使用HtmlElement.AppendChild将新元素添加到 Html 文档中,您可以通过提取doc.documentElement.outerHTML;再次将该 Html 文档写入该文件中doc.documentElement.outerHTML; in a string .在一个string

This library Html Agility might give what you are looking at.这个库Html Agility可能会提供您正在查看的内容。

Hope this gives you a way to start on this and might help you to achieve what you are looking at.希望这可以为您提供一种开始的方法,并可能帮助您实现您的目标。

EDIT: Here is something you can try using string manipulations:编辑:这是您可以尝试使用字符串操作的内容:

string column1Data = "Data 1 From UI";
string column2Data = "Data 2 From UI";
string _htmlData = System.IO.File.ReadAllText(Environment.CurrentDirectory + "\\HtmlPage1.html");
string _newRow = string.Format("<tr><td>{0}</td><td>{1}</td></tr>", column1Data, column2Data);
string _finalStr = _htmlData.Insert(_htmlData.IndexOf("</table>"), _newRow);
System.IO.File.WriteAllText(Environment.CurrentDirectory + "\\HtmlPage1.html", _finalStr);

This reads the HTML file contents, appends the new row at the end of the table and then writes back the content.这将读取 HTML 文件内容,在表格末尾追加新行,然后写回内容。

Though this may not be the optimal or right approach but hoping this solves your issue虽然这可能不是最佳或正确的方法,但希望这能解决您的问题

Happy coding...Cheers快乐编码...干杯

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

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