Reputation: 569
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*
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
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
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