mayap
mayap

Reputation: 569

Parse part of text which is html-like table in c#

I have a free text which might contains html-like definition of table, for example:

This is free text..... More free text... table start *row start*

cell 1 content# #cell 2 content

cell 3 content

row end*table end* More free text which might contain more tables definitions.

I'm looking for the best way to parse tables from such text in C#. I've read that regular expressions are not good for such text. Can any one help with this matter?

Thanks in advance.

Upvotes: 0

Views: 497

Answers (2)

Ivan Golović
Ivan Golović

Reputation: 8832

You can try it like this:

        string input = @"free text ... 
        <table><tr><td>
            <table><tr><td>test</td></tr></table>
        </td></tr></table> 
        more free text";
        string inputWithRoot = String.Format("<root>{0}</root>", input);

        XElement el = XElement.Parse(inputWithRoot);
        var tables = el.Descendants("table");

        foreach (XElement table in tables)
        {
            Console.WriteLine(table.ToString());
            Console.WriteLine();
        }

Upvotes: 2

HatSoft
HatSoft

Reputation: 11201

Once you have extracted the table in to a string

Please use Server.HtmlEncode to encode text that have html in it

Upvotes: 0

Related Questions