简体   繁体   中英

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.

I have a existing HTML file with figures in it. I want to add a table to the end of the HTML file.

The data of the table is from user input at the GUI interface(Textbox) and socket client program.

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.

The Html file is in the same folder as the C# .exe program built.

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.

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; 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; in a string .

This library Html Agility might give what you are looking at.

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.

Though this may not be the optimal or right approach but hoping this solves your issue

Happy coding...Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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