0

I'm trying to convert a two dimension list of string to an html table. I did this who does the job :

    public string htmlTableFromTwoDimensionList(List<List<string>> list)
    {
        XDocument xDocument = new XDocument(new XElement("table"));
        XElement xTable = xDocument.Element("table");

        foreach(List<string> row in list)
        {
            XElement xRow = new XElement("tr");
            foreach(string col in row)
            {

                if (list.First() == row) xRow.Add(new XElement("th", col));
                else xRow.Add(new XElement("td", col));
            }
            xTable.Add(xRow);
        }

        return xDocument.ToString();
    }

But now, i learn that the string can be some html. So i would like to parse it if it's html or use a string if it's not. I tried to do something like that, without success :

public string htmlTableFromTwoDimensionList(List<List<string>> list)
    {
        XDocument xDocument = new XDocument(new XElement("table"));
        XElement xTable = xDocument.Element("table");

        foreach(List<string> row in list)
        {
            XElement xRow = new XElement("tr");
            foreach(string col in row)
            {
                XElement content;
                string text = "";

                // tcheck if content is html or text :
                try
                {
                    content = XElement.Parse(col);
                }
                catch
                {
                    text = col;
                }

                if (list.First() == row) xRow.Add(new XElement("th", string.IsNullOrEmpty(text) ? content : text));
                else xRow.Add(new XElement("td", string.IsNullOrEmpty(text) ? content : text));
            }
            xTable.Add(xRow);
        }

        return xDocument.ToString();
    }

But I'm not even sure to use try catch in this situation. Any idea to do that properly ?

  • 1) Can you [edit] your question to share a [mcve], specifically some sample input data consisting of a mixture of HTML and text? 2) HTML is not XML. A well-formed HTML document may be malformed XML. To parse HTML as XML it generally needs to be XHTML. Are your strings XHTML, or just HTML? – dbc Nov 22 '22 at 16:44
  • thanks for the response, so the better would be an html parser, but i didn't find something like that in the standard .net libraries. – Lucas Weibel Nov 23 '22 at 08:39
  • You can use https://html-agility-pack.net/, see [How to use HTML Agility pack](https://stackoverflow.com/q/846994/3744182). – dbc Nov 23 '22 at 14:56

1 Answers1

0

here is a solution, probably not the best, with some sample input :

class Program
{
    static void Main(string[] args)
    {
        List<List<string>> table = new List<List<String>>{
            new List<String> { "1d", "Client", "some html", "Date", "col n"},
            new List<String> { "1", "Client 1","<a href=\"https://www.google.com\">google</a>","31/12/2022", "some content ..." },
            new List<String> { "2", "Client 2","<a href=\"https://www.google.com\">google</a>","31/12/2022", "some content ..." },
            new List<String> { "3", "Client 3","<a href=\"https://www.google.com\">google</a>","31/12/2022", "some content ..." },
        };

        Console.Write(htmlTableFromTwoDimensionList(table));
        Console.Read();
    }

    public static string htmlTableFromTwoDimensionList(List<List<string>> list)
    {
        XDocument xDocument = new XDocument(new XElement("table"));
        XElement xTable = xDocument.Element("table");

        foreach (List<string> row in list)
        {
            XElement xRow = new XElement("tr");
            foreach (string col in row)
            {
                XElement htmlCel;
                if (list.First() == row) htmlCel = new XElement("th");
                else htmlCel = new XElement("td");

                XElement content;

                try
                {
                    content = XElement.Parse(col);
                    htmlCel.Add(content);
                }
                catch
                {
                    htmlCel.Add(col);
                }
                xRow.Add(htmlCel);
            }
            xTable.Add(xRow);
        }

        return xDocument.ToString();
    }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 25 '22 at 10:09