7

I am using c#. I have following string

<li> 
    <a href="abc">P1</a> 
    <ul>
        <li><a href = "bcd">P11</a></li>
        <li><a href = "bcd">P12</a></li>
        <li><a href = "bcd">P13</a></li>
        <li><a href = "bcd">P14</a></li>
    </ul>
</li>
<li> 
    <a href="abc">P2</a> 
    <ul>
        <li><a href = "bcd">P21</a></li>
        <li><a href = "bcd">P22</a></li>
        <li><a href = "bcd">P23</a></li>
    </ul>
</li>
<li> 
    <a href="abc">P3</a> 
    <ul>
        <li><a href = "bcd">P31</a></li>
        <li><a href = "bcd">P32</a></li>
        <li><a href = "bcd">P33</a></li>
        <li><a href = "bcd">P34</a></li>
    </ul>
</li>
<li> 
    <a href="abc">P4</a> 
    <ul>
        <li><a href = "bcd">P41</a></li>
        <li><a href = "bcd">P42</a></li>
    </ul>
</li>

My aim is to fill the following list from the above string.

List<class1>

class1 has two properties,

string parent;
List<string> children;

It should fill P1 in parent and P11,P12,P13,P14 in children, and make a list of them.

Any suggestion will be helpful.

Edit

Sample

public List<class1> getElements()
{
    List<class1> temp = new List<class1>();
    foreach(// <a> element in string)
    {
        //in the recursive loop
        List<string> str = new List<string>();
        str.add("P11");
        str.add("P12");
        str.add("P13");
        str.add("P14");

        class1 obj = new class1("P1",str);
        temp.add(obj);
    }
    return temp;
}

the values are hard coded here, but it would be dynamic.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
RTRokzzz
  • 235
  • 1
  • 4
  • 14
  • @tim: I am getting code of generated html page of .aspx file by javascript and storing it in hiddenfield. Then It would be available to me in backend as string **sorry No webrowser control** – RTRokzzz Nov 30 '12 at 13:25
  • Could you pelase elaborate on what exactly you want as the strings in the list you are wanting to fill? So an example of 'class1' could have parent == "P1" and children = ["P11", "P22", ...]? What is supposed to be in the list? Only the links, right? Are the lists going to be nested any more (ie. P222 etc?) – K.L. Nov 30 '12 at 13:44
  • @K.L. : I just want data in children list i.e. P11,P12,.. and so on, not the values of links. You can assume it similar to innerHtml. – RTRokzzz Nov 30 '12 at 13:49
  • still not clear to me. Could you edit the question and show us a sample (say for P1) of the expected result list? Also, you didnt say if you wanted the algorithm to work for any number of nesting levels, or just the 2, like in your example. Its kinda confusing, cause in this sort of task, youd expect a TREE, not a list! – K.L. Nov 30 '12 at 14:02
  • @K.L. please see the edit. Sorry boss, If you know any of the way to convert string in HtmlElementCollection, then I can solve it. – RTRokzzz Nov 30 '12 at 14:11
  • You can download [Html Agility Pack](http://htmlagilitypack.codeplex.com/) from nuget, it will add a dll to your project as a reference, there is plenty of documentation out there – CR41G14 Nov 30 '12 at 13:24