0

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>
        &bull;
    </td>
    <td>
        Item 1
    </td>
</tr>
</table>
<table style="margin-left: 5px;">
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 1.1
        </td>
    </tr>
</table>
<table style="margin-left: 10px;">
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 1.1.1
        </td>
    </tr>
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 1.1.2
        </td>
    </tr>
</table>
<table>
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 2
        </td>
    </tr>
</table>
<table style="margin-left: 5px;">
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 2.1
        </td>
    </tr>
</table>
<table style="margin-left: 10px;">
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 2.1.1
        </td>
    </tr>
    <tr>
        <td>
            &bull;
        </td>
        <td>
            Item 2.1.2
        </td>
    </tr>
</table>

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I tried originally, but the problem is, the resulting tables can't be nested...
3

Would a regular expression replacing <ol> with <table> and <li> with <tr><td> not work?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.