Convert HTML Tags Using C#
I have a situation where I need to convert a nested HTML list like this:
<ol>
<li>Item 1
<ol>
<li> Item 1.1
<ol>
<li>Item 1.1.1</li>
<li>Item 1.1.2</li>
</ol>
</li>
</ol>
</li>
<li>Item 2
<ol>
<li> Item 2.1
<ol>
<li>Item 2.1.1</li>
<li>Item 2.1.2</li>
</ol>
</li>
</ol>
</li>
Into separate indented tables (where each ol is a table, indented properly to look like the nested table). What would be the best way to do this? I've looked that the HtmlAgility pack, but I couldn't figure out how to replace tags once I found them (I was able to find all the appropriate tags, but couldn't do anything with them)...
Basically, I need the开发者_如何学运维 output table(s) to look something like this:
<table>
<tr>
<td>
•
</td>
<td>
Item 1
</td>
</tr>
</table>
<table style="margin-left: 5px;">
<tr>
<td>
•
</td>
<td>
Item 1.1
</td>
</tr>
</table>
<table style="margin-left: 10px;">
<tr>
<td>
•
</td>
<td>
Item 1.1.1
</td>
</tr>
<tr>
<td>
•
</td>
<td>
Item 1.1.2
</td>
</tr>
</table>
<table>
<tr>
<td>
•
</td>
<td>
Item 2
</td>
</tr>
</table>
<table style="margin-left: 5px;">
<tr>
<td>
•
</td>
<td>
Item 2.1
</td>
</tr>
</table>
<table style="margin-left: 10px;">
<tr>
<td>
•
</td>
<td>
Item 2.1.1
</td>
</tr>
<tr>
<td>
•
</td>
<td>
Item 2.1.2
</td>
</tr>
</table>
Just break down what you're trying to do into simpler steps.
1) Replace all <ol>
with <table><tr>
2) Replace all <li>
with <td>
3) Replace all </li>
with </td>
4) Replace all </ol>
with </tr></table>
...Or similar. Its basically a straight up translation if I'm understanding your issue correctly.
Would a regular expression replacing <ol>
with <table>
and <li>
with <tr><td>
not work?
精彩评论